Daniel Daniel - 2 months ago 9
PHP Question

Merge the arrays

I have two arrays

$array
and
$array2
. I need to merge them on behalf of common key value i.e. entry_id.
Well I need to merge them is such a way that if the entry_id of array 1 matches with the entry_id of array2, it merges. If the entry_id doesn't match that array remains as it but it should be entered in the merged array. I have tried but i didn't get the desired results. If it is possible to do this without function?

Thanks in Advance.

Here is my code

<?php

$array = array(
array(
'title' => 'mytitleeee',
'entry_id' => 1000
),
array(
'title' => 'myt',
'entry_id' => 1001
),
array(
'title' => 'mytRRRR',
'entry_id' => 1003
),
array(
'title' => 'RUKES',
'entry_id' => 1004
)
);

$array2 = array(
array(
'author_id' => 'desc1',
'entry_id' => 1000
),
array(
'author_id' => 'desc2',
'entry_id' => 1001
),
array(
'author_id' => 'desc3',
'DAY' => 'MON',
'entry_id' => 1003
),
array(
'author_id' => 'desc7',
'DAY' => 'TUE',
'entry_id' => 1012
)

);
$x = array();
foreach($array as $value => $ans){

}
foreach($array2 as $value1 => $ans1){

}
if($ans1['entry_id']!= $ans['entry_id']){
$x = ($ans1);
echo"<pre>";
print_r($x);

}

Answer

You could apply this array_reduce call to the array_merge result:

$result = array_reduce(array_merge($array, $array2), function ($acc, $el) {
    $key = $el['entry_id'];
    $acc[$key] = isset($acc[$key]) ? $acc[$key] + $el : $el;
    return $acc;
}, []);

$result will have the following for your sample data:

array (
  1000 => array (
    'title' => 'mytitleeee',
    'entry_id' => 1000,
    'author_id' => 'desc1',
  ),
  1001 => array (
    'title' => 'myt',
    'entry_id' => 1001,
    'author_id' => 'desc2',
  ),
  1003 => array (
    'title' => 'mytRRRR',
    'entry_id' => 1003,
    'author_id' => 'desc3',
    'DAY' => 'MON',
  ),
  1004 => array (
    'title' => 'RUKES',
    'entry_id' => 1004,
  ),
  1012 => array (
    'author_id' => 'desc7',
    'DAY' => 'TUE',
    'entry_id' => 1012,
  ),
)

How it works

First the two arrays are merged:

array_merge($array, $array2)

This just appends the elements of the second array after those of the first.

This array is then passed to array_reduce, which calls the callback function -- given as argument -- for each element.

Furthermore, that function also gets an accumulated value ($acc), which in the first call is [] (provided as final argument to reduce). Whatever the function returns becomes the accumulated value that is passed in the second function call (for the second element), ...etc. The final returned value becomes the return value of reduce.

So in this case the accumulated value is an associative array that is keyed by entry_id. If at a certain moment that key already exists, the current value is merged with the value that is already in $acc: this merge is done with the + operator. If the entry_id of the current element is not yet in $acc it is added to it.

This if...else is implemented with the ternary operator (... ? ... : ...).

The return$accstatement ensures that the the next time this callback is called (for the next element),$acc` is again that accumulated value.

Comments