Javascript 向 JSON 编码数组添加其他对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3504278/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Additional Objects to JSON Encoded Array
提问by Dodinas
I am currently using a JSON encoded array to display the users in my database for an auto-suggest feature.
我目前正在使用 JSON 编码的数组来显示我的数据库中的用户以获取自动建议功能。
It looks something like this:
它看起来像这样:
$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);
This returns:
这将返回:
[{"id":"81","name":"John Doe"},{"id":"82","name":"Jane Doe"}]
My question is somewhat 2-fold:
我的问题有两方面:
First, how would I manually add an additional object to this output?  For example, let's say I wanted to add:  {"id":"444","name":"A New Name"}
首先,我将如何手动向这个输出添加一个额外的对象?例如,假设我想添加:  {"id":"444","name":"A New Name"}
Thus, it'd look like:
因此,它看起来像:
[{"id":"81","name":"John Doe"},{"id":"82","name":"Jane Doe"},{"id":"444","name":"A New Name"}]
Second, let's say I also wanted to add more objects to the array from a separate table as well, such as:
其次,假设我还想从单独的表中向数组添加更多对象,例如:
$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);
This way I could have both tables populated in the JSON array, thus, showing up as additional options in my autosuggest.
这样我就可以在 JSON 数组中填充两个表,因此,在我的自动建议中显示为附加选项。
Hopefully this makes sense, as I've tried hard to articulate what I am trying to accomplish.
希望这是有道理的,因为我一直在努力阐明我要完成的工作。
Thanks!
谢谢!
回答by meder omuraliev
Just keep pushing to the $dataarray.
只需继续推动$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;
Then at the very end, do your json_encode. Assuming you're not referring to merging it in the JS itself with multiple ajax calls.
然后在最后,做你的json_encode. 假设您不是指将它与多个 ajax 调用合并到 JS 本身中。
And if you have separate scripts, combine them in one php page.
如果您有单独的脚本,请将它们组合在一个 php 页面中。
回答by vineet
Try in this way:-
以这种方式尝试:-
$temp = json_encode($json);  //$json={"var1":"value1","var2":"value2"}   
$temp=substr($temp,0,-1);
$temp.=',"variable":"'.$value.'"}';
回答by Colin Fine
You could edit the JSON (text), but it's much easier to modify the array before you encode it. Or am I missing something?
您可以编辑 JSON(文本),但在编码之前修改数组要容易得多。或者我错过了什么?
回答by XstreamINsanity
I'm not an expert in any of these fields, but I'll try and see if I can help. Try one of these:
我不是这些领域的专家,但我会尝试看看是否可以提供帮助。尝试以下方法之一:
Option 1 (I don't know how unions work in mysql):
选项 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);
I've never done PHP, so I'm making assumptions. I've also never used MySql, so there's more assumptions.
我从来没有做过 PHP,所以我在做假设。我也从未使用过 MySql,所以有更多的假设。
Option 2:
选项 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);
Hope this helps.
希望这可以帮助。

