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

2 Answers 2

2

Try this: (assuming $old_array is your 3-dim array)

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

Using array_shift() instead of reset() will also remove the first element from the array so notice that.

I've used array_merge() since you asked "and assign it to another array", however if you're ok with both arrays being the same one you can drop the array_merge() and stick to:

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

Check this working fiddle

Sign up to request clarification or add additional context in comments.

8 Comments

same thing, im getting the error: Strict Standards: Only variables should be passed by reference in C:\public_html\songbook\index.php on line 52
@Marco which is line 52?
the error is because i try to enclose two or more functions that deals with an array type of variable, php will return an error..
tried it, after fixing the php error, and this solution does return the last first array... thanks!!
@Marco beware of this.. it will not always work. it will work only if the internal pointer of the inner array is set to the first element
|
0

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

var_dump($arr_first);

This way you can get always first independent on id.

2 Comments

im getting this error: Strict Standards: Only variables should be passed by reference in C:\public_html\songbook\index.php on line 48
ok the problem is that when i try to enclose two or more functions that deals with an array type of variable, php will return an error.. That beeing said, this solution returns the first 2 dimensional array.. i want to last array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.