tags:

views:

2060

answers:

8

I have two arrays of animals (for example).

$array = array(
    array(
     'id' => 1,
     'name' => 'Cat',
    ),
    array(
     'id' => 2,
     'name' => 'Mouse',
    )
);

$array2 = array(
    array(
     'id' => 2,
     'age' => 321,
    ),
    array(
     'id' => 1,
     'age' => 123,
    )
);

How can I merge the two arrays into one by the ID?

+1  A: 

First off, why don't you use the ID as the index (or key, in the mapping-style array that php arrays are imo)?

$array = array(
    1 => array(
        'name' => 'Cat',
    ),
    2 => array(
        'name' => 'Mouse',
    )
);

after that you'll have to foreach through one array, performing array_merge on the items of the other:

foreach($array2 as $key=>$value) {
  if(!is_array($array[$key])) $array[$key] = $value;
  else $array[$key] = array_merge($array[key], $value); 
}

Something like that at least. Perhaps there's a better solution?

Erik van Brakel
+1  A: 

This does what Erik suggested (id no. as array key) and merges vlaues in $array2 to $results.

$results = array();

foreach($array as $subarray)
{
    $results[$subarray['id']] = array('name' => $subarray['name']);
}

foreach($array2 as $subarray)
{
    if(array_key_exists($subarray['id'], $results))
    {
        // Loop through $subarray would go here if you have extra 
        $results[$subarray['id']]['age'] = $subarray['age'];
    }
}
Ross
A: 

First off, why don't you use the ID as the index (or key, in the mapping-style array that php arrays are imo)?

~Erik van Brakel

I would use the index as the ID but I can't as I'm getting the data back from databases and they are indexed by row number. The databases are two different servers as well so I cant join them in the query.

Annan
A: 

    $new = array();
    foreach ($array as $arr) {
        $match = false;
        foreach ($array2 as $arr2) {
            if ($arr['id'] == $arr2['id']) {
               $match = true;
               $new[] = array_merge($arr, $arr2);
               break;
            }
        }
        if ( !$match ) $new[] = $arr;
    }
Andy
+2  A: 

@Andy

http://se.php.net/array_merge

That was my first thought but it doesn't quite work - however array_merge_recursive might work - too lazy to check right now.

Ross
A: 

@Andy

I've already looked at that and didn't see how it can help merge multidimensional arrays. Maybe you could give an example.

@kevin

That is probably what I will need to do as I think the code below will be very slow. The actual code is a bit different because I'm using ADOdb (and ODBC for the other query) but I'll make it work and post my own answer.

This works, however I think it will be very slow as it goes through the second loop every time:

foreach($array as &$animal)
{
    foreach($array2 as $animal2)
    {
     if($animal['id'] === $animal2['id'])
     {
      $animal = array_merge($animal, $animal2);
      break;
     }
    }
}
Annan
A: 

@Annan: I already provided an example. Check the post I edited.

Andy
A: 
    <?php
$a = array('a' => '1', 'b' => array('t' => '4', 'g' => array('e' => '8')));
$b = array('c' => '3', 'b' => array('0' => '4', 'g' => array('h' => '5', 'v' => '9')));
$c = array_merge_recursive($a, $b);
print_r($c);
?>

outputs:

        Array
(
    [a] => 1
    [b] => Array
        (
            [t] => 4
            [g] => Array
                (
                    [e] => 8
                    [h] => 5
                    [v] => 9
                )

            [0] => 4
        )

    [c] => 3
)
Unkwntech