views:

75

answers:

4

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

$array1 = array ( 
                  0 => array ( 'id' => 1, 'name' => 'somename') ,
                  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') 
);
+2  A: 

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]);
}
hsz
good. i'd add $number = min(count($array1), count($array2)); and then just do $i < $number in the loop
brian_d
Feel free. :)))
hsz
A: 

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'
}
Austin Fitzpatrick
+1  A: 

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.

aib
A: 
$new_array = array($array1, $array2);
Adam Bailin