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:

  1. $sth = mysql_query("SELECT id, name FROM users");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['name'];
  5. $json['id'] = $row['id'];
  6. $data[] = $json;
  7. }
  8. 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:

  1. $sth = mysql_query("SELECT id, title FROM another_table");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['title'];
  5. $json['id'] = $row['id'];
  6. $data[] = $json;
  7. }
  8. 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!

share | improve this question
 
 
And why don't you add them to your array before you do the json_encode? That would make more sense to me. –   wimvds  Aug 17 '10 at 15:46 

3 Answers

up vote 8 down vote accepted

Just keep pushing to the $data array.

  1. $json = array();
  2. while($row = mysql_fetch_assoc($sth)) {
  3. $json['name'] = $row['name'];
  4. $json['id'] = $row['id'];
  5. $data[] = $json;
  6. }
  7. $custom = array('name'=>'foo', 'id' => 'bar');
  8. $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.

share | improve this answer
 
 
Thanks Meder. Easier than I thought it'd be. –   Dodinas  Aug 17 '10 at 17:04

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

share | improve this answer
 

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. $sth = mysql_query("SELECT id, name FROM users union SELECT id, name FROM (SELECT id, title as name from another_table) as T2");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['name'];
  5. $json['id'] = $row['id'];
  6. }
  7. $json['name'] = 'A new name';
  8. $json['id'] = '444';
  9. $data[] = $json;
  10. 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:

  1. $sth = mysql_query("SELECT id, name FROM users");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['name'];
  5. $json['id'] = $row['id'];
  6. }
  7. $sth = mysql_query("SELECT id, title from another_table");
  8. while($row = mysql_fetch_assoc($sth)) {
  9. $json['name'] = $row['title'];
  10. $json['id'] = $row['id'];
  11. }
  12. $json['name'] = 'A new name';
  13. $json['id'] = '444';
  14. $data[] = $json;
  15. print json_encode($data);

Hope this helps.

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:

  1. $sth = mysql_query("SELECT id, name FROM users");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['name'];
  5. $json['id'] = $row['id'];
  6. $data[] = $json;
  7. }
  8. 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:

  1. $sth = mysql_query("SELECT id, title FROM another_table");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['title'];
  5. $json['id'] = $row['id'];
  6. $data[] = $json;
  7. }
  8. 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!

share | improve this question
 
    
And why don't you add them to your array before you do the json_encode? That would make more sense to me. –   wimvds  Aug 17 '10 at 15:46 

3 Answers

up vote 8 down vote accepted

Just keep pushing to the $data array.

  1. $json = array();
  2. while($row = mysql_fetch_assoc($sth)) {
  3. $json['name'] = $row['name'];
  4. $json['id'] = $row['id'];
  5. $data[] = $json;
  6. }
  7. $custom = array('name'=>'foo', 'id' => 'bar');
  8. $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.

share | improve this answer
 
    
Thanks Meder. Easier than I thought it'd be. –   Dodinas  Aug 17 '10 at 17:04

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

share | improve this answer
 

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. $sth = mysql_query("SELECT id, name FROM users union SELECT id, name FROM (SELECT id, title as name from another_table) as T2");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['name'];
  5. $json['id'] = $row['id'];
  6. }
  7. $json['name'] = 'A new name';
  8. $json['id'] = '444';
  9. $data[] = $json;
  10. 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:

  1. $sth = mysql_query("SELECT id, name FROM users");
  2. $json = array();
  3. while($row = mysql_fetch_assoc($sth)) {
  4. $json['name'] = $row['name'];
  5. $json['id'] = $row['id'];
  6. }
  7. $sth = mysql_query("SELECT id, title from another_table");
  8. while($row = mysql_fetch_assoc($sth)) {
  9. $json['name'] = $row['title'];
  10. $json['id'] = $row['id'];
  11. }
  12. $json['name'] = 'A new name';
  13. $json['id'] = '444';
  14. $data[] = $json;
  15. print json_encode($data);

Hope this helps.