Skip to content
Advertisement

How to find first/second element of associative array when keys are unknown?

In PHP when you have an associative array, e.g.:

JavaScript

what is the syntax to access the first or second element of the array when you don’t know the value of the keys?

Is there something like in a C# LINQ statement where you can say:

JavaScript

or

JavaScript

Or do I have to use a foreach statement and pick them out as I do at the bottom of this code example:

JavaScript

Advertisement

Answer

use these functions to set the internal array pointer:

http://ch.php.net/manual/en/function.reset.php

http://ch.php.net/manual/en/function.end.php

And this one to get the actual element: http://ch.php.net/manual/en/function.current.php

JavaScript

If you wanna have the last/first key then just do something like $tmp = array_keys($groups); .

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement