PHP:多维数组合并递归

时间:2014-09-07 15:53:44

标签: php arrays multidimensional-array array-merge

我需要合并这两个数组:

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

预期产出:

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

但它输出:

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

我正在寻找最简单的方法,我的数组输入不会比这些例子更深。

2 个答案:

答案 0 :(得分:22)

在这里。

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

答案 1 :(得分:0)

感谢Meglio的注释,您可以将这些函数用于任意数量的数组:

功能

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

用法

$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);

用法(测试数据)

$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,
];

用法输出(两个功能相同)

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