user1292810
user1292810

Reputation: 1822

Append a new associative element (with values drawn from a flat lookup array) to all rows of a 2d array

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 then 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?

Upvotes: 0

Views: 1893

Answers (5)

mickmackusa
mickmackusa

Reputation: 47864

You can use array_map() to traverse your rows, then use the array union operator to append the new element. Demo

var_export(
    array_map(
        fn($row) => $row + ['new_key' => $map[$row['id']]],
        $rows
    )
);

Upvotes: 0

Leri
Leri

Reputation: 12535

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.

Upvotes: 2

Prasanth Bendra
Prasanth Bendra

Reputation: 32730

<?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);
?>

Upvotes: 0

Baba
Baba

Reputation: 95101

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)

Upvotes: 5

Venkata Krishna
Venkata Krishna

Reputation: 4305

You can simple do like this also

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

        $arr1[$key]['new_key'] = isset($arr2[$key])?$arr2[$key]:'';
}

Upvotes: 0

Related Questions