如何在PHP中组合数组?

时间:2011-12-10 00:53:28

标签: php arrays multidimensional-array

我有这些数组:

$ ARRAY1

Array
            (
                [0] => Array
                    (
                        [state] => AE
                        [state_pri] => 0
                    )

                [1] => Array
                    (
                        [state] => AK
                        [state_pri] => 0
                    )

                [2] => Array
                    (
                        [state] => AL
                        [state_pri] => 0
                    )
)

$数组2

Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [count1] => 0
                            )

                    )

                [1] => Array
                    (
                        [0] => Array
                            (
                                [count1] => 1
                            )

                    )

                [2] => Array
                    (
                        [0] => Array
                            (
                                [count1] => 18
                            )

                    )
)

$ ARRAY3

Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [count] => 0
                            )

                    )

                [1] => Array
                    (
                        [0] => Array
                            (
                                [count] => 1
                            )

                    )

                [2] => Array
                    (
                        [0] => Array
                            (
                                [count] => 18
                            )

                    )
)

......我想得到这样的东西:

Array
            (
                [0] => Array
                    (
                        [state] => AE
                        [state_pri] => 0
                        [0] => Array
                            (
                                [count] => 0
                            )
                        [0] => Array
                            (
                                [count1] => 0
                            )
                    )

                [1] => Array
                    (
                        [state] => AK
                        [state_pri] => 0
                        [1] => Array
                            (
                                [count] => 0
                            )
                        [1] => Array
                            (
                                [count1] => 0
                            )
                    )

                [2] => Array
                    (
                        [state] => AL
                        [state_pri] => 0
                        [2] => Array
                            (
                                [count] => 0
                            )
                        [2] => Array
                            (
                                [count1] => 0
                            )
                    )
)

关于如何做到这一点的任何想法?

编辑:只是为了添加更多代码,如果我使用array_merge,我会得到:

Array
            (
                [0] => Array
                    (
                        [state] => AE
                        [state_pri] => 0
                    )

                [1] => Array
                    (
                        [state] => AK
                        [state_pri] => 0
                    )

                [2] => Array
                    (
                        [state] => AL
                        [state_pri] => 0
                    )
                [3] => Array
                    (
                        [0] => Array
                            (
                                [count] => 0
                            )

                    )

                [4] => Array
                    (
                        [0] => Array
                            (
                                [count] => 1
                            )

                    )

                [5] => Array
                    (
                        [0] => Array
                            (
                                [count] => 18
                            )

                    )
                 .....
)

4 个答案:

答案 0 :(得分:1)

你要找的东西仍然有点难以辨认。如果这能达到预期效果,请告诉我们:

foreach ($array1 as $key => $val) {
  $array1[$key][] = $array3[$key];
  $array1[$key][] = $array2[$key];
}

print_r($array1);

答案 1 :(得分:0)

答案 2 :(得分:0)

$merged = array();
foreach (array($array1,$array2,$array3) as $array) {
    foreach ($array as $key=>$value) {
        if (!isset($merged[$key])) {
            $merged[$key] = array();    
        }
        $merged[$key] += $value; 
    }
}
print_r($merged);

答案 3 :(得分:0)

$combined=$array0 + $array1;

试一下