PHP: Get first and last elements of an array

I often need to grab the first or last element of an array. This method works for either numeric or associative arrays.

$array = array('cats','dogs','fish','horses','squids');

$last  = end($array);   # 'squids'
$first = reset($array); # 'cats'
Leave A Reply
All content licensed under the Creative Commons License