正确获取 JSON 编码 - PHP


Correct fetching for JSON encode - PHP

我有一个简单的问题。我正在从数据库中获取数据,并通过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 ) )

线路回波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"}]

我应该在代码中更改什么才能实现此目的?

你能试试吗:

$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);

你在这里做的事情似乎有点奇怪。您正在迭代一个多维数组,按键提取值,然后使用相同的键将这些值添加到新数组中,并将它们构建回多维数组中。您开头的数组应与以数组结尾的数组相同,除非此处的代码中缺少其他内容。

也就是说,您呈现的格式应该按预期工作。如果$professors数组结构如下所示:

$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);