PHP:数值数组叠加的递归方法

-数值数组叠加的递归方法:几个相同格式的数组的对应项叠加,得到一个此种格式的数组
-@param array(数组1, 数组2, ...)
-@return array 数组为上面数组1,数组2,...的对应项叠加

function ToOne($arr1, $arr2) {
    foreach ($arr1 as $k1 => $v1) {
        if (!is_array($v1)) {
            $arr1[$k1] = $arr1[$k1] + $arr2[$k1];
        } else {
            $arr1[$k1] = ToOne($arr1[$k1], $arr2[$k1]);
        }       
    }
    return $arr1;
}

$arr1 = [
    1,
    'zsh' => [
        2,
        'zsk' => [
            3,
            'dihezsh' => [
                4,
                'dihezsk' => 5
            ]
        ]
    ]
];

$arr2 = [
    1,
    'zsh' => [
        2,
        'zsk' => [
            3,
            'dihezsh' => [
                4,
                'dihezsk' => 5
            ]
        ]
    ]
];

echo '
';
print_r($arr1);
echo "
"; echo '
';
print_r(ToOne($arr1, $arr2));

你可能感兴趣的:(PHP:数值数组叠加的递归方法)