我如何合并这些数组?

时间:2011-07-14 11:32:10

标签: php

我有这个数组结构:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [timestamp] => 1310394569
                    [title] => SimpleXMLElement Object
                        (
                            [0] => A static tweet here from the static xml
                        ) 

                       [link] => SimpleXMLElement Object
                       (
                            [0] => http://www.google.com
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => static
                        )

                )

        )

    [1] => Array
        (
            [0] => Array
               (
                    [timestamp] => 1310117641
                    [title] => SimpleXMLElement Object
                        (
                        )

                    [link] => SimpleXMLElement Object
                    (
                        [0] => http://www.facebook.com/
                    )

                [type] => SimpleXMLElement Object
                    (
                        [0] => facebook
                    )

            )

        [1] => Array
            (
                [timestamp] => 1309856547
                [title] => SimpleXMLElement Object
                    (
                    )

                [link] => SimpleXMLElement Object
                    (
                        [0] => http://www.facebook.com/
                    )

                [type] => SimpleXMLElement Object
                    (
                        [0] => facebook
                    )

            )

但是我想摆脱外部包含的数组...所以我留下了这个:

Array
(
     [0] => Array
            (
                [timestamp] => 1310394569
                [title] => SimpleXMLElement Object
                    (
                        [0] => A static tweet here from the static xml
                    ) 

                   [link] => SimpleXMLElement Object
                   (
                        [0] => http://www.google.com
                    )

                [type] => SimpleXMLElement Object
                    (
                        [0] => static
                    )

            )

       [1] => Array
            (
                [timestamp] => 1310394569
                [title] => SimpleXMLElement Object
                    (
                        [0] => A static tweet here from the static xml
                    ) 

                   [link] => SimpleXMLElement Object
                   (
                        [0] => http://www.google.com
                    )

                [type] => SimpleXMLElement Object
                    (
                        [0] => static
                    )

            )      

数组合并根本没有做任何事情....有什么办法可以做到这一点吗?

2 个答案:

答案 0 :(得分:1)

你的例子有点不清楚,但假设你要转这个

array(
    array(
         array(…)
    )
    array(
         array(…)
    )
)

array(
     array(…)
     array(…)
)

这样做:

$array = array_map('current', $array);

如果你想转过来

array(
    array(
        array(…)
    )
    array(
        array(…)
        array(…)
    )
)

array(
    array(…)
    array(…)
    array(…)
)

这应该做:

$array = array_reduce($array, function ($result, $array) {
             return array_merge($result, $array);
         }, array());

答案 1 :(得分:0)

对于多维数组,您必须使用array_merge_recursive