Last active
December 30, 2015 02:49
-
-
Save l-x/7765283 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* @link https://l-x.github.io/blog/2014/09/03/determine-the-type-of-a-php-array/ | |
* | |
* @param array $array | |
* | |
* @return int | |
*/ | |
function get_array_type(array $array) { | |
$keyvalue_count = count(array_filter(array_keys($array), 'is_string')); | |
switch ($keyvalue_count) { | |
case 0: return 0; // completely indexed or empty array | |
case count($array): return 1; // completely key=>value based array | |
default: return 2; // mixed array | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment