PHP : multidimensional array merge recursive

牧云@^-^@ 提交于 2021-02-18 10:25:29

问题


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.


回答1:


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;
}



回答2:


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


来源:https://stackoverflow.com/questions/25712099/php-multidimensional-array-merge-recursive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!