dourao1877 2015-03-11 18:46
浏览 68

正确获取JSON编码 - PHP

I have simple problem. I am fetching data from database, and I am sending it to Javascript client over Ajax. I am having trouble with formatting array that needs to be sent. My code:

$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 looks like this:

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) gives this:

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"}]

However it should look only like this:

[{"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"}]

What should I change in my code to achieve this?

  • 写回答

2条回答 默认 最新

  • doushao8399 2015-03-11 18:55
    关注

    Can you try that:

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?