Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : multidimensional array merge recursive

I need to merge those two arrays:

$ar1 = array("color" => array("red", "green"), "aa");
$ar2 = array("color" => array( "green", "blue"), "bb");
$result = array_merge_recursive($ar1, $ar2);

Expected output:

[
    'color' => [
        (int) 0 => 'red',
        (int) 1 => 'green',
        (int) 3 => 'blue'
    ],
    (int) 0 => 'aa',
    (int) 1 => 'bb'
]

But it outputs:

[
    'color' => [
        (int) 0 => 'red',
        (int) 1 => 'green',
        (int) 2 => 'green', (!)
        (int) 3 => 'blue'
    ],
    (int) 0 => 'aa',
    (int) 1 => 'bb'
]

I'm looking for the simplest way to do this, my array inputs won't be deeper than those examples.

like image 785
hjrshng Avatar asked Sep 07 '14 15:09

hjrshng


People also ask

What is array_merge_recursive() function in PHP?

The array_merge_recursive () is an inbuilt function in PHP and is used to merge two or more arrays into a single array recursively. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array.

How to merge multidimensional arrays in PHP?

How to merge multidimensional arrays in PHP? If you want to join two multidimensional arrays in PHP, you should still use array_merge, and not array_merge_recursive. Confused? So was I.

What is the use of array_merge_recursive in Python?

Definition and Usage. The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key.

How to combine two arrays with different keys in PHP?

Example: So in the following code we have declared two different arrays with different keys and we have combined them using the array_merge () PHP array_merge_recursive (): The array_merge_recursive () function in PHP is a type of function that is used to merge or combine one or many arrays into one single array.


2 Answers

Here it is.

function array_merge_recursive_ex(array $array1, array $array2)
{
    $merged = $array1;

    foreach ($array2 as $key => & $value) {
        if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
            $merged[$key] = array_merge_recursive_ex($merged[$key], $value);
        } else if (is_numeric($key)) {
             if (!in_array($value, $merged)) {
                $merged[] = $value;
             }
        } else {
            $merged[$key] = $value;
        }
    }

    return $merged;
}
like image 193
Mark.Ablov Avatar answered Sep 17 '22 15:09

Mark.Ablov


Thanks to Meglio comment, you can use these functions for any number of arrays :

Functions

function drupal_array_merge_deep() {
  $args = func_get_args();
  return drupal_array_merge_deep_array($args);
}

// source : https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_array_merge_deep_array/7.x
function drupal_array_merge_deep_array($arrays) {
    $result = array();
    foreach ($arrays as $array) {
        foreach ($array as $key => $value) {
            // Renumber integer keys as array_merge_recursive() does. Note that PHP
            // automatically converts array keys that are integer strings (e.g., '1')
            // to integers.
            if (is_integer($key)) {
                $result[] = $value;
            }
            elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
                $result[$key] = drupal_array_merge_deep_array(array(
                    $result[$key],
                    $value,
                ));
            }
            else {
                $result[$key] = $value;
            }
        }
    }
    return $result;
}

Usage

$merged = drupal_array_merge_deep($ar_1, $ar_2);

var_dump($merged);

$merged = drupal_array_merge_deep_array([$ar_1, $ar_2]);

var_dump($merged);

Usage (test data)

$ar_1 = [
    "item1" => false,
    "item2" => true,
    "item_list" => [
        "sub_item1" => 5,
        "sub_itemlist" => [
            "sub_sub_item1" => 27,
        ],
    ]
];

$ar_2 = [
    "item_list" => [
        "sub_item2" => 5,
        "sub_itemlist" => [
            "sub_sub_item2" => 27,
        ],
    ],
    "item3" => true,
];

Usage output (same for both functions)

array (size=4)
  'item1' => boolean false
  'item2' => boolean true
  'item_list' => 
    array (size=3)
      'sub_item1' => int 5
      'sub_itemlist' => 
        array (size=2)
          'sub_sub_item1' => int 27
          'sub_sub_item2' => int 27
      'sub_item2' => int 5
  'item3' => boolean true
like image 37
Samuel Dauzon Avatar answered Sep 19 '22 15:09

Samuel Dauzon