Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP utility: find the first non-array element
/**
* First Leaf
* Returns the first non-array element (singular or object).
* Handy when you're not sure whether information has been stored
as a singular value ('item') or 1-element array (0=>'item').
* The key() function gives us the first key in the array.
* Uses recursion to go deeper if necessary; if that's not
* what you want, you may need a different solution.
*/
public static function first_leaf ( $value ) {
return !is_array( $value ) ? $value : first_leaf( $value[key($value)] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.