0

given the following 3 dimensional array as an example:

Array
(
[324] => Array
    (
        [0] => Array
            (
                [workid] => 324
                [wname] => Y quiero llegar a más / Para triunfar en la vía
                [recordingid] => 476
                [rname] => Y quiero llegar a más / Para triunfar en la vía
                [rsubname] => Fandangos sevillanos
            )

    )

[325] => Array
    (
        [0] => Array
            (
                [workid] => 325
                [wname] => Ya basta
                [recordingid] => 479
                [rname] => Ya basta
                [rsubname] => 
            )

    )

[326] => Array
    (
        [0] => Array
            (
                [workid] => 326
                [wname] => Y sin embargo te quiero
                [recordingid] => 563
                [rname] => Y sin embargo te quiero
                [rsubname] => 
            )

        [1] => Array
            (
                [workid] => 326
                [wname] => Y sin embargo te quiero
                [recordingid] => 562
                [rname] => Y sin embargo te quiero
                [rsubname] => (continuación)
            )

        [2] => Array
            (
                [workid] => 326
                [wname] => Y sin embargo te quiero
                [recordingid] => 478
                [rname] => Y sin embargo te quiero
                [rsubname] => 
            )

    )
)

i want to pick the first array of the 3 dimensional array and assign it to another array, let's call it $arr_first

the outpout of $arr_first should look like this

Array
                (
                    [workid] => 324
                    [wname] => Y quiero llegar a más / Para triunfar en la vía
                    [recordingid] => 476
                    [rname] => Y quiero llegar a más / Para triunfar en la vía
                    [rsubname] => Fandangos sevillanos
                )

Sure i could loop through and get what i want. But i need to know What is the most elegant and fastest way to achieve this whithout looping the entire array?

EDIT: The array is just an example... it's hard coded. The key values could change. So i need to get the first array independent on id

4

2 回答 2

2

试试这个:(假设$old_array是你的 3-dim 数组)

$arr_first = array_merge(array(), reset(reset($old_array))); // merge with empty array to clone
print_r($arr_first);

使用array_shift()而不是reset()也会从数组中删除第一个元素,所以请注意。

自从您询问“并将其分配给另一个数组”以来,我就一直在使用array_merge(),但是如果您对两个数组都可以接受相同的数组,则可以放弃array_merge()并坚持:

 $arr_first = reset(reset($old_array));

检查这个工作小提琴

于 2013-08-21T20:18:43.483 回答
0

$arr_first = array_shift(array_values($array));

var_dump($arr_first);

这样你就可以总是首先独立于 id。

于 2013-08-21T20:22:21.690 回答