Skip to content

Instantly share code, notes, and snippets.

@l-x
Last active December 30, 2015 02:49
Show Gist options
  • Save l-x/7765283 to your computer and use it in GitHub Desktop.
Save l-x/7765283 to your computer and use it in GitHub Desktop.
<?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