【问题标题】:Correct fetching for JSON encode - PHP正确获取 JSON 编码 - PHP
【发布时间】: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"}]

我应该在我的代码中更改什么来实现这一点?

【问题讨论】:

    标签: php json database


    【解决方案1】:

    你能试试吗:

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

    【讨论】:

      【解决方案2】:

      你在做什么似乎有点奇怪。您正在迭代多维数组,通过键提取值,然后使用相同的键将值添加到新数组并将它们重新构建到多维数组中。您开始的数组应该与您结束的数组相同,除非此处的代码中缺少其他内容。

      也就是说,您提供的格式应该可以按预期工作。如果您的 $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);
      

      【讨论】:

        猜你喜欢
        • 2017-09-12
        • 1970-01-01
        • 2012-09-07
        • 2021-12-31
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多