【发布时间】:2015-03-11 18:46:47
【问题描述】:
我有一个简单的问题。我正在从数据库中获取数据,并通过 Ajax 将其发送到 Javascript 客户端。我在格式化需要发送的数组时遇到问题。 我的代码:
$queryData = $this->execute_query("SELECT tema, opis, id FROM predlogtema WHERE (idpred = '$oblast' AND idprof = '$profesor' AND odabrana = '0')",1);
foreach ($queryData as $key => $value) {
$tema=$queryData[$key]['tema'];
$opis=$queryData[$key]['opis'];
$id=$queryData[$key]['id'];
$profesors[] = array
(
'tema' => ($tema),
'opis' => ($opis),
'id'=>($id)
);
}
echo json_encode($profesors);
数组 $queryData 如下所示:
Array ( [0] => Array ( [tema] => tema1 [opis] => opis [id] => 1 ) [1] => Array ( [tema] => tema1 [opis] => fsdfds [id] => 4 ) [2] => Array ( [tema] => fsd [opis] => fsdf [id] => 6 ) [3] => Array ( [tema] => ewqrwqr [opis] => etwretrewtre [id] => 21 ) [4] => Array ( [tema] => ewqre [opis] => rewtrwert [id] => 23 ) [5] => Array ( [tema] => ava [opis] => aba [id] => 26 ) [6] => Array ( [tema] => prob1 [opis] => prrobaaa [id] => 30 ) )
Line echo json_encode($profesors) 给出了这个:
Array
(
[0] => Array
(
[tema] => tema1
[opis] => opis
[id] => 1
)
[1] => Array
(
[tema] => tema1
[opis] => fsdfds
[id] => 4
)
[2] => Array
(
[tema] => fsd
[opis] => fsdf
[id] => 6
)
[3] => Array
(
[tema] => ewqrwqr
[opis] => etwretrewtre
[id] => 21
)
[4] => Array
(
[tema] => ewqre
[opis] => rewtrwert
[id] => 23
)
[5] => Array
(
[tema] => ava
[opis] => aba
[id] => 26
)
[6] => Array
(
[tema] => prob1
[opis] => prrobaaa
[id] => 30
)
)
[{"tema":"tema1","opis":"opis","id":"1"},{"tema":"tema1","opis":"fsdfds","id":"4"},{"tema":"fsd","opis":"fsdf","id":"6"},{"tema":"ewqrwqr","opis":"etwretrewtre","id":"21"},{"tema":"ewqre","opis":"rewtrwert","id":"23"},{"tema":"ava","opis":"aba","id":"26"},{"tema":"prob1","opis":"prrobaaa","id":"30"}]
但是它应该看起来像这样:
[{"tema":"tema1","opis":"opis","id":"1"},{"tema":"tema1","opis":"fsdfds","id":"4"},{"tema":"fsd","opis":"fsdf","id":"6"},{"tema":"ewqrwqr","opis":"etwretrewtre","id":"21"},{"tema":"ewqre","opis":"rewtrwert","id":"23"},{"tema":"ava","opis":"aba","id":"26"},{"tema":"prob1","opis":"prrobaaa","id":"30"}]
我应该在我的代码中更改什么来实现这一点?
【问题讨论】: