I would like to peek at the first element of an array. This operation would be equivalent to this code:
function peek($list)
{
$item = array_shift($list);
array_unshift($list, $item);
return $item;
}
This code just seems really heavy to me and peek is often provided by queue and stack libraries. Does php have an already built function or some more efficient way to do this? I searched php.net but was unable to find anything.
Additional note for clarity: The array is not necessarily numerically indexed. It is also possible the array may have had some items unset (in the case of a numerically indexed array) messing up the numerical ordering. It is not safe to assume $list[0] is the first element.