Skip to content

Instantly share code, notes, and snippets.

@paulhhowells
Created April 16, 2013 14:55
Show Gist options
  • Save paulhhowells/5396606 to your computer and use it in GitHub Desktop.
Save paulhhowells/5396606 to your computer and use it in GitHub Desktop.
array_key_split()
/*
* supply an array with integer and string indexes
* and get an associative array divided into #string & #integer
* or if a key_type argument is supplied then get an array with only the keys that match it
*
* arguments:
* $key_type is optional, and may be 'integer' or 'string'
*
* for use by process_tested_class()
*/
function array_key_split($array, $key_type = NULL) {
$return_array = FALSE;
$integer_keys = array();
$string_keys = array();
foreach($array as $key => $value) {
if (is_numeric($key)) { // perhaps a test for integers is slower than a numeric test
$integer_keys[] = $value;
} else {
$string_keys[$key] = $value;
}
}
if (empty($key_type)) {
$return_array = array ();
$return_array['integer'] = $integer_keys;
$return_array['string'] = $string_keys;
} else {
if ($key_type == 'integer') {
$return_array = $integer_keys;
} elseif ($key_type == 'string') {
$return_array = $string_keys;
}
}
return $return_array;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment