0

I have two table that has multiple rows. I want to combine those two tables rows into one long array which will be identified as one array I wrote this code

$posts_sql = $db->query("SELECT * FROM posts WHERE id < $lastpost AND position = submitter order by id DESC LIMIT 5");
$posts_all = $db->fetch_all($posts_sql);
foreach($posts_all as $key => $posts_row){
$users_sql = $db->query("SELECT username,firstname,lastname,avatar FROM users WHERE username = '".$posts_row['submitter']."'");
$users_all = $db->fetch_assoc($users_sql);
$data[] = $posts_row;
$data[] = $users_all;
}

echo json_encode($data);

It makes duplicate arrays doesn't right...

That's how my result show

[{
"id":"39",
"hash":"070fcc8e73ba5f549f87",
"post":"hello\n",
"files":"",
"location":", 
"GB","status":"1",
"position":"dabblos",
"submitter":"dabblos",
"source":"text",
"ip":"37.130.227.133",
"stamp":"1390197699"
},
{
"username":"dabblos",
"firstname":"dabb",
"lastname":"los",
"avatar":"no_avatar.png"
}]

please help me make it just one long array

I would like to see the output looks like this

{
"id":"39",
"hash":"070fcc8e73ba5f549f87",
"post":"hello\n",
"files":"",
"location":", 
"GB","status":"1",
"position":"dabblos",
"submitter":"dabblos",
"source":"text",
"ip":"37.130.227.133",
"stamp":"1390197699"
"username":"dabblos",
"firstname":"dabb",
"lastname":"los",
"avatar":"no_avatar.png"
}
8
  • say i wanted to render avatar, it render it twice if used IMG tag html Commented Apr 14, 2014 at 3:19
  • can you show us, how you wanted the output look like? Commented Apr 14, 2014 at 3:24
  • It is a quirk of json_encode, if you look at the encode options in the manual: function.json-encode there is no way to force the write of an array, it writes objects instead. However, When TRUE, returned objects will be converted into associative arrays. ] Commented Apr 14, 2014 at 4:10
  • how about if we union all the two tables? Commented Apr 14, 2014 at 4:12
  • 1
    glad it all worked out :-) Commented Apr 14, 2014 at 4:35

3 Answers 3

2

look at this,i have taken example values, it is working fine as you wanted

$arr=array(array("abc"=>"1","def"=>"2"),array("abcc"=>"11","deff"=>"22"));

echo json_encode($arr);
$final = array();
foreach($arr as $item) {
    $final = array_merge($final, $item);
}
print_r($final);

output

[{"abc":"1","def":"2"},{"abcc":"11","deff":"22"}]//json_array

Array ( [abc] => 1 [def] => 2 [abcc] => 11 [deff] => 22 )//final array

UPDATE

json_encode the final array and you'll get the desired result

echo json_encode($final);

output

{"abc":"1","def":"2","abcc":"11","deff":"22"}
Sign up to request clarification or add additional context in comments.

3 Comments

that's how my current output looks like [{"abc":"1","def":"2"},{"abcc":"11","deff":"22"}] I want only this {"abc":"1","def":"2","abcc":"11","deff":"22"}
you have to json encode the finall array,see my update in answer
May i suggest, that since you scanned each element of the array anyway, then using 'array_merge' is not as clear as $target[] = $source. In efficiency terms, as PHP has a 'compiler' pass, i suspect the code generated is identical anyway. me being pedantic :-(
1

Untested:

$posts_sql = $db->query("SELECT * FROM posts WHERE id < $lastpost AND position = submitter order by id DESC LIMIT 5");
$posts_all = $db->fetch_all($posts_sql);
foreach($posts_all as $key => $posts_row) {
   $users_sql = $db->query("SELECT username,firstname,lastname,avatar FROM users WHERE username = '".$posts_row['submitter']."'");
   $users_all = $db->fetch_assoc($users_sql);
   $data[] = $posts_row;  
   foreach($users_all as $user) 
    $data[] = $user; 
   } 
}


echo json_encode($data); 

// when you use json_decode use the 'true' flag as in 
// $decodedJson =  json_decode($json, true);

1 Comment

i'm only getting the first row of each table
0

Merge them before you json_encode them:

$data[] = $posts_row;
$data2[] = $users_all;

$result = array_merge($data,$data2);

echo json_encode($result);

1 Comment

nope, that actually made it worse... now it looping through data1 before it jumps to data2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.