Created
September 2, 2011 14:46
-
-
Save anotheremily/1188794 to your computer and use it in GitHub Desktop.
array_peek
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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