4

I have two arrays that need to be merged. I can loop through each array and merge manually. But, Is there a built in function to do this?

Array 1:

Array
(
[0] => Array
    (
        [detail_image_id] => 6389
        [product_name] => Starter broadband Package
    )

[1] => Array
    (
        [detail_image_id] => 6358
        [product_name] => Starter broadband Package
    )
)

Array 2:

Array
(
[0] => Array
    (
        [detail_image_id] => 6358
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
    )

[1] => Array
    (
        [detail_image_id] => 6389
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
    )
)

Expected Output array is:

Array
(
[0] => Array
    (
        [detail_image_id] => 6389
        [product_name] => Starter broadband Package
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
    )

[1] => Array
    (
        [detail_image_id] => 6358
        [product_name] => Starter broadband Package
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
    )
)

I have added only a sample from the array. Array is huge. Is there any PHP functions like array_merge() or array_map() that we can use instead of manual for loops and iterators?

Syscall
  • 19,327
  • 10
  • 37
  • 52
Dilani
  • 533
  • 1
  • 6
  • 22
  • Maybe [array_merge_recursive](http://php.net/manual/en/function.array-merge-recursive.php) or [array_replace_recursive](http://php.net/manual/en/function.array-replace-recursive.php) – Michel May 17 '18 at 06:29

3 Answers3

4

Older & wiser: I've scrubbed my answer from years earlier because I no longer recommend the techniques. It will be most succinct and efficient to merge the arrays, feed them to a foreach() loop, then "unite" (+ is used as an array union operator) data in related rows.

The null coalescing operator (??) is used to ensure that there is always something to "unite" with.

Code: (Demo)

$result = [];
foreach (array_merge($array1, $array2) as $row) {
    $result[$row['detail_image_id']] = ($result[$row['detail_image_id']] ?? []) + $row;
}

var_export(array_values($result));

Output:

array (
  0 => 
  array (
    'detail_image_id' => '6389',
    'product_name' => 'Starter broadband Package',
    'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg',
  ),
  1 => 
  array (
    'detail_image_id' => '6358',
    'product_name' => 'Starter broadband Package',
    'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
3

You can do something like:

$arr1 = array(); //Your array 1
$arr2 = array(); //Your array 2

//Make a temp array to that use detail_image_id as the key
$arr1Temp = array_combine( array_column($arr1,'detail_image_id'), $arr1 );
$arr2Temp = array_combine( array_column($arr2,'detail_image_id'), $arr2 );

//Get All unique detail_image_id from 2 arrays. 
//This is to make sure that all detail_image_id's will be included. 
//detail_image_id on 2nd array might not be present on the 1st one
$imgIds = array_unique(array_merge( array_keys($arr1Temp), array_keys($arr2Temp) ));

//Do a simple foreach loop
$result = array();
foreach( $imgIds as $val ) {
    $result[] = array_merge( $arr1Temp[$val], $arr2Temp[$val] );
}

print_r( $result );

This will result to:

Array
(
    [0] => Array
        (
            [detail_image_id] => 6389
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
        )

    [1] => Array
        (
            [detail_image_id] => 6358
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
        )

)
Eddie
  • 26,593
  • 6
  • 36
  • 58
3

You can use a combination of array_column() and array_combine() to create a reference array using the detail_image_id as key. Then, with a foreach() loop, you can merge arrays using + operator:

$array1 = [
    ['detail_image_id' => '6389', 'product_name' => 'Starter broadband Package'],
    ['detail_image_id' => '6358', 'product_name' => 'Starter broadband Package']
];
$array2 = [
    ['detail_image_id' => '6358', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg'],
    ['detail_image_id' => '6389', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg']
];

// create associative array with detail_image_id as key,
$out = array_combine(array_column($array1, 'detail_image_id'), $array1);
foreach ($array2 as $item) {
    $key = $item['detail_image_id']; // shortcut for the key,
    $out[$key] = $out[$key] + $item; // merge arrays
}
print_r(array_values($out)); // reset to indexed keys (0,1,2...) 

Output:

Array
(
    [0] => Array
        (
            [detail_image_id] => 6389
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
        )

    [1] => Array
        (
            [detail_image_id] => 6358
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
        )

)
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • For future use, please add `array_column()` with a `null` 2nd parameter in your bag of tricks -- it's a handy one. – mickmackusa May 21 '18 at 05:13
  • p.s. Your answer has a _possible_ weakness in that it is iterating on `array2` and `array1` may have more values (which will be silently omitted from the result). Eddie has factored this into his answer. – mickmackusa May 21 '18 at 05:15