简体   繁体   中英

Merging two arrays with the same keys

I need a help with merging two arrays. Let's say I have following arrays:

Array
(
[3] => Array
    (
        [id] => 3
        [name] => Lorem
        [type] => pl
    )

[2] => Array
    (
        [id] => 2
        [name] => Ipsum
        [type] => pl
    )

)

And the second one:

Array
(
    [6] => Sit dolor
    [4] => nostrud exercitation ullamco
    [3] => uuntur magni dolores
    [2] => corporis suscipit laboriosam
    [7] =>  quia non numquam eius modi tempora
 )

And desired output is:

Array
(
[3] => Array
    (
        [id] => 3
        [name] => Lorem
        [type] => pl
        [new_key] => uuntur magni dolores
    )

[2] => Array
    (
        [id] => 2
        [name] => Ipsum
        [type] => pl
        [new_key] => corporis suscipit laboriosam
    )
)

I have been trying to compare arrays with array_diff_key() method and than merge arrays in some loop, but I could't get it working. Is there any built in PHP function, or should I write my own? If so, could you help me with getting desired result? Any help is much appreciated.

All you need is

$data = array_map(function ($v) use($new) {
    isset($new[$v['id']]) and $v['new_key'] = $new[$v['id']] ;
    return $v;
}, $data);

var_dump($data);

Output ( DEMO )

array (size=2)
  3 => 
    array (size=4)
      'id' => int 3
      'name' => string 'Lorem' (length=5)
      'type' => string 'pl' (length=2)
      'new_key' => string 'uuntur magni dolores' (length=20)
  2 => 
    array (size=4)
      'id' => int 2
      'name' => string 'Ipsum' (length=5)
      'type' => string 'pl' (length=2)
      'new_key' => string 'corporis suscipit laboriosam' (length=28)

You can simply iterate over first array:

foreach ($arr1 as $key=> $item) {
    $arr1[$key]['new_key'] = isset($arr2[$item['id']]) ? $arr2[$item['id']] : '';
}

Or

foreach ($arr1 as &$item) { //just for fun
    $item['new_key'] = isset($arr2[$item['id']]) ? $arr2[$item['id']] : '';
}

But it seems that your data comes from database. If I am right, you'd better use sql JOIN .

You can simple do like this also

foreach($arr1 as $key => $arr) {

        $arr1[$key]['new_key'] = isset($arr2[$key])?$arr2[$key]:'';
}
<?php
$first_array  = Array("3" => Array("id" => 3,"name" => "Lorem","type" => "pl"),
                      "2" => Array("id" => 2,"name" => "Ipsum","type" => "pl")
                     );

$sec_array   = Array("6" => "Sit dolor",
                    "4" => "nostrud exercitation ullamco",
                    "3" => "uuntur magni dolores",
                    "2" => "corporis suscipit laboriosam",
                    "7" =>  "quia non numquam eius modi tempora",
                 );

$res_array   = array();
foreach($first_array as $key=>$val){
     if(array_key_exists($key,$sec_array)){
        $val["new_key"] = $sec_array[$key];
     }
     $res_array[$key]   = $val;
}

echo "<pre>";
print_r($res_array);
?>

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