开发者

how do I merge these arrays?

开发者 https://www.devze.com 2023-03-20 11:53 出处:网络
I have this array structure: Array ( [0] => Array ( [0] => Array ( 开发者_StackOverflow[timestamp] => 1310394569

I have this array structure:

Array
(
    [0] => Array
        (
            [0] => Array
                (
   开发者_StackOverflow                 [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
                    )

            )

But I want to get rid of the outer containing array...so I'm left with this:

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
                    )

            )      

array merge isn't doing anything at all....is there any way to do this?


Your example is a bit unclear, but assuming you want to turn this

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

into

array(
     array(…)
     array(…)
)

this'll do:

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

If you want to turn this

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

into

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

this should do:

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


For multidimensional arrays you have to use array_merge_recursive.

0

精彩评论

暂无评论...
验证码 换一张
取 消