PHP Array Merge Two Arrays on Same Key

Merging arrays with the same keys

Try with array_merge_recursive

$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);

echo "<pre>";
print_r($c);
echo "</pre>";

will return

Array
(
[a] => 1
[b] => 2
[c] => Array
(
[0] => 3
[1] => 4
)

[d] => 5
)

Merging two PHP arrays with same numeric key

You can try below code to merge array. Code generates desired output required to you. I have used sample array as given by you:

<?php
$arr1=array(
"384"=>array("name"=>"SomeMovieName1","age"=>"12.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
"452"=>array("name"=>"SomeMovieName2","age"=>"15.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
"954"=>array("name"=>"SomeMovieName3","age"=>"4.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>"")
);
$arr2=array(
"384" => array("IMDBLink" => "7.2", "IMDBRating" => "http://www.imdb.com/LinkToMovie1", "coverArt" => "http://www.SomeLinkToCoverArt.com/1"),
"452" => array("IMDBLink" => "5","IMDBRating" => "http://www.imdb.com/LinkToMovie2", "coverArt" => "http://www.SomeLinkToCoverArt.com/2"),
"954"=>array("IMDBLink" => "8","IMDBRating" => "http://www.imdb.com/LinkToMovie3", "coverArt" => "http://www.SomeLinkToCoverArt.com/3")
);
$arr3 = array();
foreach($arr1 as $key=>$val)
{
$arr3[] = array_merge($val, $arr2[$key]);
}
echo "<pre>";
print_r($arr3);
?>

Combining values from two arrays with same keys

If you can guarantee these two arrays will always have the same keys:

$arr3 = array();
foreach ($arr1 as $key => $val1) {
$val2 = $arr2[$key];
$arr3[$key] = $val1 . "-" . $val2;
}

Now, assuming the two have different keys and you want to do a "joined" array with only the keys that exist in both arrays:

$arr3 = array();
foreach (array_intersect(array_keys($arr1), array_keys($arr2)) as $key) {
$arr3[$key] = $arr1[$key] . "-" . $arr2[$key];
}

This is similar to a SQL inner join.

If you want to do a "joined" array with the keys in either of the arrays, hyphen-joining only the ones that exist in both:

$arr3 = array();
foreach (array_merge(array_keys($arr1), array_keys($arr2)) as $key) {
if (isset($arr1[$key]) && isset($arr2[$key])) {
$arr3[$key] = $arr1[$key] . "-" . $arr2[$key];
} else {
$arr3[$key] = (isset($arr1[$key]) ? $arr1[$key] : $arr2[$key]);
}
}

This is similar to a SQL outer join.

Try all of the above online!

Merge two arrays, replacing values of same key

You need to use array_replace_recursive

$listskilssresult = [
[
352 => 1,
128 => '',
64 => '',
70 => 2
]
];

$listskilssresultmatching = [
[
128 => 1.5
]
];


print_r(array_replace_recursive($listskilssresult, $listskilssresultmatching));

Prints :

Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)

)

Know the difference between array_replace_recursive and array_merge_recursive here

Merge two 2d arrays on shared value from different keys

Deceze's solution is perfect if the two arrays are synchronized (i.e. items with matching code and model are at the same index in both arrays). If that is not the case you will need more than one line of code.

The arrays need to have string keys in order to be merged with array_merge. In this case you want to rekey the first one based on code and the second based on model. After rekeying you can merge them with array_merge_recursive.

Rekeying is easy in PHP 5.5+:

// array_column will conveniently rekey when the second argument is null
$array1 = array_column($array1, null, 'code');
$array2 = array_column($array2, null, 'model');

A bit more complicated in earlier versions:

// array_map is a more complicated way to extract a column from an array
// and array_combine will do the rekeying
$array1 = array_combine(
array_map(function($i) { return $i['code']; }, $array1),
$array1);
$array2 = array_combine(
array_map(function($i) { return $i['model']; }, $array2),
$array2);

In both cases the final step would be the same:

$result = array_merge_recursive($array1, $array2);

How to combine 2 arrays based on same value of same key

If you reindex your second array by name first, it will be easier to get those values from it.

$second = array_column($second, null, 'name');

(It's okay to do this if 'name' is unique in the array. I assume that's the case since you said it's "like an ID". If 'name' isn't unique then you'll lose some rows when you reindex, because array keys have to be unique.)

Then iterate your first array and merge any corresponding values in the second array into the result.

foreach ($first as $key => $value) {
if (isset($second[$value['name']])) {
$result[$key] = array_merge($value, $second[$value['name']]);
}
}

This would be like an inner join in SQL, where the result would only include rows where the value exists in both tables. If you wanted it to work more like a left join, then you'd need to merge a set of empty values for the keys in the second array if a matching name value wasn't found there.

foreach ($first as $key => $value) {
if (isset($second[$value['name']])) {
$result[$key] = array_merge($value, $second[$value['name']]);
} else {
$result[$key] = array_merge($value, ['menu' => null, 'key' => null]);
}
}

Working example at 3v4l.org.

PHP merge two arrays on the same key AND value

Try out this code, It might help you, it's short and without traversing loops:

    usort($array2,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
$array3 = array_replace_recursive($array1, $array2);

$result = array_uintersect($array3,$array1,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
print_r($result);

Output

Array
(
[0] => Array
(
[ur_user_id] => 1
[ur_fname] => PerA
[ur_lname] => SonA
[ur_code] => AA
[ur_user_role] => testA
)

[1] => Array
(
[ur_user_id] => 2
[ur_fname] => PerB
[ur_lname] => SonB
[ur_code] => BB
[ur_user_role] => testB
)

[2] => Array
(
[ur_user_id] => 3
[ur_fname] => PerC
[ur_lname] => SonC
[ur_code] => CC
[ur_user_role] => testC
)

)

And Here is Demo

PHP Array Merge two Arrays on same key

Ideally I would look to format the two arrays in such a way that array_merge_recursive would simply merge the arrays without too much fuss.

However I did come up with a solution that used array_map.

$array1 = array(
array("Camera1" => "192.168.101.71"),
array("Camera2" => "192.168.101.72"),
array("Camera3" => "192.168.101.74"),
);

$array2 = array(
array("Camera1" => "VT"),
array("Camera2" => "UB"),
array("Camera3" => "FX")
);

$results = array();

array_map(function($a, $b) use (&$results) {

$key = current(array_keys($a));
$a[$key] = array('ip' => $a[$key]);

// Obtain the key again as the second array may have a different key.
$key = current(array_keys($b));
$b[$key] = array('name' => $b[$key]);

$results += array_merge_recursive($a, $b);

}, $array1, $array2);

var_dump($results);

The output is:

array (size=3)
'Camera1' =>
array (size=2)
'ip' => string '192.168.101.71' (length=14)
'name' => string 'VT' (length=2)
'Camera2' =>
array (size=2)
'ip' => string '192.168.101.72' (length=14)
'name' => string 'UB' (length=2)
'Camera3' =>
array (size=2)
'ip' => string '192.168.101.74' (length=14)
'name' => string 'FX' (length=2)


Related Topics



Leave a reply



Submit