If I had an array like:
$array['foo'] = 400;
$array['bar'] = 'xyz';
And I wanted to get the first item out of that array without knowing the key for it, how would I do that? Is there a function for this?
If I had an array like:
$array['foo'] = 400;
$array['bar'] = 'xyz';
And I wanted to get the first item out of that array without knowing the key for it, how would I do that? Is there a function for this?
You could use array_shift
Fake loop that breaks on the first iteration:
Or use
each()
(warning: deprecated as of PHP 7.2.0):you can just use
$array[0]
. it will give you the first item alwaysThere's a few options.
array_shift()
will return the first element, but it will also remove the first element from the array.current()
will return the value of the array that its internal memory pointer is pointing to, which is the first element by default.If you want to make sure that it is pointing to the first element, you can always use
reset()
.Just so that we have some other options:
reset($arr);
good enough if you're not trying to keep the array pointer in place, and with very large arrays it incurs an minimal amount of overhead. That said, there are some problems with it:The way to do this without changing the pointer:
The benefit of
$arr[reset(array_keys($arr))];
is that it raises an warning if the array is actually empty.PHP < 7.3
If you don't know enough about the array (you're not sure whether the first key is foo or bar) then the array might well also be, maybe, empty.
So it would be best to check, especially if there is the chance that the returned value might be the boolean FALSE:
The above code uses
reset
and so has side effects (it resets the internal pointer of the array), so you might prefer usingarray_slice
to quickly access a copy of the first element of the array:Assuming you want to get both the key and the value separately, you need to add the fourth parameter to
array_slice
:To get the first item as a pair (
key => value
):Simple modification to get the last item, key and value separately:
performance
If the array is not really big, you don't actually need
array_slice
and can rather get a copy of the whole keys array, then get the first item:If you have a very big array, though, the call to
array_keys
will require significant time and memory more thanarray_slice
(both functions walk the array, but the latter terminates as soon as it has gathered the required number of items - which is one).A notable exception is when you have the first key which points to a very large and convoluted object. In that case
array_slice
will duplicate that first large object, whilearray_keys
will only grab the keys.PHP 7.3
PHP 7.3 implements
array_key_first()
as well asarray_key_last()
. These are explicitly provided to access first and last keys efficiently without resetting the array's internal state as a side effect.So in PHP 7.3 the first value of
$array
may be accessed withYou still had better check that the array is not empty though, or you will get an error: