简体   繁体   中英

Add Additional Objects to JSON Encoded Array

I am currently using a JSON encoded array to display the users in my database for an auto-suggest feature.

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

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.

Hopefully this makes sense, as I've tried hard to articulate what I am trying to accomplish.

Thanks!

Just keep pushing to the $data array.

$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.

And if you have separate scripts, combine them in one php page.

Try in this way:-

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

You could edit the JSON (text), but it's much easier to modify the array before you encode it. Or am I missing something?

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

$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.

Option 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM