我希望自己的表名被内存到JSON,并且不想使用mysql表名。 如果我重命名列,我只需要在de php文件中重命名它们。
简而言之:
这就是我现在所拥有的:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rows['feed'] = $row;
sendResponse(200, json_encode($rows));
}
我想知道:如何打开获取的数组并更改列名并将它们重命名为我自己的名称,然后将它们发送给JSON
编辑: 我编辑了我的行
$result = mysql_query("SELECT * FROM od_common.debiteur WHERE SORT_NAAM LIKE '%comp%'");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$deb_nr = $row['DEB_NR'];
$deb_naam = $row['DEB_NAAM'];
$deb_adres = $row['DEB_ADRES'];
$rows['klant'] = array('klantnr' => $deb_nr, 'klntnm' => $deb_naam, 'adrs' => $deb_adres);
sendResponse(200, json_encode($rows));
}
}
我得到了我想要的最多。但我想要这个:
{
"klant": [
{
"klntnr": "10010",
"klntnm": "Company1",
"adrs": "street1"
},
{
"klntnr": "25071",
"klntnm": "Company2",
"adrs": "street2"
},
{
"klntnr": "25247",
"klntnm": "Company3",
"adrs": "street3"
},
{
"klntnr": "25454",
"klntnm": "Company4",
"adrs": "street4"
},
{
"klntnr": "25601",
"klntnm": "Company5",
"adrs": "street5"
}
]
}
不是这个:
{ "klant": {
"klantnr": "10010",
"klntnm": "Company1",
"adrs": "street1"
}
}{
"klant": {
"klantnr": "25071",
"klntnm": "Company2",
"adrs": "street2"
}
}{
"klant": {
"klantnr": "25247",
"klntnm": "Company3",
"adrs": "street3"
}
}{
"klant": {
"klantnr": "25454",
"klntnm": "Company4",
"adrs": "street4"
}
}{
"klant": {
"klantnr": "25601",
"klntnm": "Company5",
"adrs": "street5"
}
}
答案 0 :(得分:5)
最好的想法是在源代码修改您的查询,例如:
$result = mysql_query("SELECT columna, columnb FROM table");
为:
$result = mysql_query("SELECT columna AS 'whatever', column AS 'ha' FROM table");
下一个最好的想法是修改$row
数组,但这是非常基本的PHP。