开发者

joining / merging two arrays

开发者 https://www.devze.com 2022-12-28 07:34 出处:网络
I have two arrays like this, actually this is mysql data retrieved from two different servers: $array1 = array (

I have two arrays like this, actually this is mysql data retrieved from two different servers:

$array1 = array ( 
                  0 => array ( 'id' => 1, 'name' => 'somena开发者_如何学Gome') ,
                  1 => array ( 'id' => 2, 'name' => 'somename2') 
);
$array2 = array ( 
                  0 => array ( 'thdl_id' => 1, 'otherdate' => 'spmethings') ,
                  1 => array ( 'thdl_id' => 2, 'otherdate' => 'spmethings22') 
);

how can i join / merge array so it looks like this

$new_array = array ( 
         0 => array ( 'id' => 1, 'name' => 'somename', 'otherdate' => 'spmethings') ,
         1 => array ( 'id' => 2, 'name' => 'somename2', 'otherdate' => 'spmethings22') 
);


Something like that + check if their sizes are the same if you want..

$res = array()
for ( $i = 0; $i < count($array1); ++$i )
{
  $res[] = array_merge($array1[$i], $array2[$i]);
}


Like an INNER JOIN? You'll have to do it manually. I know PHP has quite a collection of exotic functions, but none do what you want as far as I know.

Think "insertion sort." Sort both arrays and walk them. Merge rows as you go.


It is possible that I'm misunderstanding, but is this what you're looking for?

for ($i = 0; $i < count($array1); $i++){
    $new_array[$i] = array_merge($array1[$i], $array2[$i]);
    unset($new_array[$i]['thdl_id']); //since i'm assuming this is a duplicate of 'id'
}


$new_array = array($array1, $array2);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号