按键合并多个数组

时间:2016-08-23 11:46:15

标签: php arrays

对我来说这不容易解释,但我试试。 我必须创建一个多维数组,用键合并相同的数组,

我的主阵列是:

[A] => Array
    (
        [A1] => 
        [A2] => 
        [A3] => 
        [A4] => 
        [A5] => 
    )
[A1] => Array
    (
        [A1a] => 
        [A1b] => 
        [A1c] => 
        [A1d] => 
        [A1f] => 
    )
[A1a] => Array
    (
        [A1a1] => 
        [A1a2] => 
        [A1a3] => 
        [A1a4] => 
        [A1a5] => 
    )
[A1a1] => value

所以,我需要的是:

[A] => Array
    (
    [A1] => Array
        (
        [A1a] => Array
            (
                [A1a1] => value
            )
        )

这是我试过的:

$final = array();
foreach($content as $key => $value){
    $final[$key] = glue($content, $key);
}

function glue($content, $i){
    $data = '';
    if(isset($content[$i])) {
        $data = $content[$i];
        if(is_array($data)){
            foreach($data as $cl => $value) {
                $data[$cl] = glue($content, $cl);
            }
        }
    }

    return $data;

}

随着我的想法得到了我想要但结束键是空的:

[A14] => Array
(
    [A14a] => Array
        (
            [7461*] => 
        )

    [A14b] => Array
        (
            [A14b1] => Array
                (
                    [A14b11] => Array
                        (
                            [7600*] => 
                            [7601*] => 
                        )

                    [A14b12] => Array
                        (
                            [7602*] => 
                            [7603*] => 
                        )

                )

1 个答案:

答案 0 :(得分:0)

您可以在数组中使用array_merge_recursive合并项。 如果你谷歌这个

  

php按键合并多个数组   你会得到结果这也是duplicate question