【问题标题】:Add Additional Objects to JSON Encoded Array向 JSON 编码数组添加其他对象
【发布时间】:2011-03-31 01:50:26
【问题描述】:

我目前正在使用 JSON 编码的数组来显示我的数据库中的用户,以实现自动建议功能。

看起来像这样:

$sth = mysql_query("SELECT id, name FROM users");

$json = array();

    while($row = mysql_fetch_assoc($sth)) {
        $json['name'] = $row['name'];
        $json['id'] = $row['id'];
        $data[] = $json;
    }

print json_encode($data);

这会返回:

[{"id":"81","name":"John Doe"},{"id":"82","name":"Jane Doe"}]

我的问题有点二:

首先,我如何手动添加一个额外的对象到这个输出?例如,假设我想添加:{"id":"444","name":"A New Name"}

因此,它看起来像:

[{"id":"81","name":"John Doe"},{"id":"82","name":"Jane Doe"},{"id":"444","name":"A New Name"}]

其次,假设我还想从单独的表中向数组中添加更多对象,例如:

$sth = mysql_query("SELECT id, title FROM another_table");

$json = array();

    while($row = mysql_fetch_assoc($sth)) {
        $json['name'] = $row['title'];
        $json['id'] = $row['id'];
        $data[] = $json;
    }

print json_encode($data);

这样我可以在 JSON 数组中填充两个表,因此,在我的自动建议中显示为附加选项。

希望这是有道理的,因为我已经努力表达我想要完成的目标。

谢谢!

【问题讨论】:

  • 为什么不在 json_encode 之前将它们添加到数组中呢?这对我来说更有意义。

标签: php javascript arrays json


【解决方案1】:

继续推送到$data 数组。

$json = array();

    while($row = mysql_fetch_assoc($sth)) {
        $json['name'] = $row['name'];
        $json['id'] = $row['id'];
        $data[] = $json;
    }

$custom = array('name'=>'foo', 'id' => 'bar');
$data[] = $custom;

然后在最后,做你的json_encode。假设您不是指将其与多个 ajax 调用合并到 JS 本身中。

如果您有单独的脚本,请将它们组合到一个 php 页面中。

【讨论】:

  • 谢谢梅德。比我想象的要容易。
【解决方案2】:

这样试试:-

$temp = json_encode($json);  //$json={"var1":"value1","var2":"value2"}   
$temp=substr($temp,0,-1);
$temp.=',"variable":"'.$value.'"}';

【讨论】:

    【解决方案3】:

    您可以编辑 JSON(文本),但在编码之前修改数组要容易得多。 还是我错过了什么?

    【讨论】:

      【解决方案4】:

      我不是这些领域的专家,但我会尝试看看能否提供帮助。尝试以下方法之一:

      选项1(我不知道联合在mysql中是如何工作的):

      $sth = mysql_query("SELECT id, name FROM users union SELECT id, name FROM (SELECT id, title as name from another_table) as T2"); 
      
      
          $json = array(); 
      
              while($row = mysql_fetch_assoc($sth)) { 
                  $json['name'] = $row['name']; 
                  $json['id'] = $row['id']; 
              }
      
          $json['name'] = 'A new name';
          $json['id'] = '444';
          $data[] = $json; 
      
          print json_encode($data);
      

      我从来没有做过 PHP,所以我在做一些假设。我也从来没有用过MySql,所以有更多的假设。

      选项 2:

      $sth = mysql_query("SELECT id, name FROM users"); 
      
      
          $json = array(); 
      
              while($row = mysql_fetch_assoc($sth)) { 
                  $json['name'] = $row['name']; 
                  $json['id'] = $row['id']; 
              }
      
      $sth = mysql_query("SELECT id, title from another_table"); 
      
      
              while($row = mysql_fetch_assoc($sth)) { 
                  $json['name'] = $row['title']; 
                  $json['id'] = $row['id']; 
              }
      
          $json['name'] = 'A new name';
          $json['id'] = '444';
          $data[] = $json; 
      
          print json_encode($data);
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2013-09-05
        • 2019-11-25
        • 2022-01-11
        • 2015-04-30
        • 2018-09-28
        • 2012-08-21
        • 2013-10-14
        • 2017-08-04
        • 1970-01-01
        相关资源
        最近更新 更多