Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created October 31, 2011 17:42
Show Gist options
  • Save gavinblair/1328120 to your computer and use it in GitHub Desktop.
Save gavinblair/1328120 to your computer and use it in GitHub Desktop.
Get first element of an array
<?php
//Get value of first element in array, without modifying the array
$first_value = array_shift(array_values($array));
//Get key of first element
$first_key = array_shift(array_keys($array));
@SeanJA
Copy link

SeanJA commented Oct 31, 2011

Why not:

function array_first_value($array){
    $array = reset($array);
    return $array;
}

for the value?

This one works for the last value:

function array_last_value($array){
    $array = end($array);
    return $array;
}

I wonder what the speed difference is...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment