Concatenate / merge two arrays

I have two arrays like this, actually this is the mysql data obtained 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 / concatenate an array so that 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 source to share


4 answers


I may be wrong, but is this what you are 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'
}

      

0


a source


Something like this + check if their sizes are the same.



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

      

+2


a source


How INNER JOIN

? You will need to do this manually. I know PHP has quite a bunch of exotic features, but nobody does what you want as far as I know.

Think "insertion sort". Sort both arrays and loop through them. Concatenate lines as you go.

+1


a source


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


a source







All Articles