Skip to content

Instantly share code, notes, and snippets.

@anotheremily
Created September 2, 2011 14:46
Show Gist options
  • Save anotheremily/1188794 to your computer and use it in GitHub Desktop.
Save anotheremily/1188794 to your computer and use it in GitHub Desktop.
array_peek
<?php
/**
* Peeks at the first element of an array. Optionally returns the first key
* if the 2nd parameter is set to true.
*
* @param Array $arr
* @param Boolean $returnKey
* @return Mixed $val
*/
function array_peek($arr, $key = false) {
if ($key === true) {
$val = key($arr);
} else {
$val = array_slice($arr, 0, 1);
$val = $val[0];
}
return $val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment