我有一个简单的问题。我从数据库中获取数据,并通过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);
Array $ 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 ) )
行回显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"}]
为了达到这个目的,我应该在代码中进行哪些更改?
答案 0 :(得分:0)
你能试试吗:
$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' => utf8_encode($tema),
'opis' => utf8_encode($opis),
'id'=> utf8_encode($id)
);
}
echo json_encode($profesors);
答案 1 :(得分:0)
你在做什么似乎有点奇怪。您正在迭代一个多维数组,通过键拉出值,然后使用相同的键将值添加到新数组并将它们构建回多维数组。您开始使用的数组应该与您结束的数组相同,除非此处的代码中缺少其他内容。
那说你提出的格式应该按预期工作。如果您的$ professorors数组结构如下:
$array = array(
array(
'tema' => 'tema1',
'opis' => 'opis',
'id' => 1
),
array(
'tema' => 'tema1',
'opis' => 'fsdfds',
'id' => 4
),
array(
'tema' => 'fsd',
'opis' => 'fsdf',
'id' => 6
),
array(
'tema' => 'fsd',
'opis' => 'fsdf',
'id' => 6
),
array(
'tema' => 'ewqrwqr',
'opis' => 'etwretrewtre',
'id' => 21
),
array(
'tema' => 'ewqre',
'opis' => 'rewtrwert',
'id' => 23
),
array(
'tema' => 'ava',
'opis' => 'aba',
'id' => 26
),
array(
'tema' => 'prob1',
'opis' => 'prrobaaa',
'id' => 30
));
使用时的输出:
echo json_encode($professors);
应该是:
[{"tema":"tema1","opis":"opis","id":1},{"tema":"tema1","opis":"fsdfds","id":4},{"tema":"fsd","opis":"fsdf","id":6},{"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}]
以下代码将为您提供预期结果:
foreach ($queryData as $key => $value) {
$profesors[] = array
(
'tema' => $value['tema'],
'opis' => $value['opis'],
'id'=> $value['id']
);
}
echo json_encode($profesors);