简体   繁体   中英

How to combine two or more mysql array in one

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

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

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

Merge them before you json_encode them:

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

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

echo json_encode($result);

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