递归地将元素添加到数组

时间:2018-05-16 12:37:25

标签: php arrays merge

我需要合并这两个数组($array2中的$array1):

$array1 = array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => array('value1', 'value1_1'),
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
        ...
    ),
);

$array2 = array(
    'data' => array(
       array(
           'raw' => array(
               'key1' => 'value1_2',
               'key2' => 'value2',
           )
       )
    )
);

预期结果:

array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => array('value1', 'value1_1', 'value1_2'),
                'key2' => 'value2',
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
    ),
);

当我使用array_merge_recursive$array2中合并$array1['data'][0]时,我得到了以下结果:

$array1 = array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => array('value1', 'value1_1'),
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
        array(
           'raw' => array(
               'key1' => 'value1_2',
               'key2' => 'value2',
           )
       )
    ),
);

当我使用array_replace_recursive时,我得到了以下结果:

$array1 = array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => 'value1_2',
                'key2' => 'value2',
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
    ),
);

我正在寻找获得预期结果的最简单方法。 数组可以具有未知的deph数据结构,并且它可以具有多个索引子数组。我似乎需要array_merge_recursivearray_replace_recursive

之间的组合

非常感谢您的回答:)

1 个答案:

答案 0 :(得分:2)

首先需要做一些准备工作,以清除那些不需要的索引子阵列。

代码:(Demo

$array1['data']=current($array1['data']); // repair
$array2['data']=current($array2['data']); // repair
var_export(array_merge_recursive($array1, $array2));  // merge as intended

输出:

array (
  'data' => 
  array (
    'raw' => 
    array (
      'key1' => 
      array (
        0 => 'value1',
        1 => 'value1_1',
        2 => 'value1_2',
      ),
      'key3' => 'value3',
      'key2' => 'value2',
    ),
  ),
)

在问题更新后表达更复杂的数据结构...

代码:(Demo

foreach ($array1['data'] as $set) {
    foreach ($set['raw'] as $k => $v) {
        if (!isset($result['data']['raw'][$k])) {
            $result['data']['raw'][$k] = $v;
        }else {
            $result['data']['raw'][$k] = array_merge((array)$result['data']['raw'][$k],(array)$v);
        }
    }
}
foreach ($array2['data'] as $set) {
    foreach ($set['raw'] as $k => $v) {
        if (!isset($result['data']['raw'][$k])) {
            $result['data']['raw'][$k] = $v;
        }else{
            $result['data']['raw'][$k] = array_merge((array)$result['data']['raw'][$k],(array)$v);
        }
    }
}
var_export($result);