Skip to content

Instantly share code, notes, and snippets.

@attitude
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attitude/9768220 to your computer and use it in GitHub Desktop.
Save attitude/9768220 to your computer and use it in GitHub Desktop.
Function to check if array is associative (key-value)
{
"name": "attitude/Functions/is_assoc_array",
"autoload": {
"files": ["function-is_assoc_array.php"]
}
}
<?php
/**
* Function to check if array is associative (key-value)
*
* @param array $array Array to check
* @param bool $speedy Default true, pass false to use less memory
* @returns bool True if associative, false if sequential
* @author Alix Axel <http://stackoverflow.com/users/89771/alix-axel>
* @see http://stackoverflow.com/questions/173400
* @license CC BY-SA 3.0) <http://creativecommons.org/licenses/by-sa/3.0/>
*
*/
function is_assoc_array($array, $speedy=true)
{
if ($speedy) {
return ($array !== array_values($array));
}
// More memory efficient
return $array = array_keys($array); return ($array !== array_keys($array));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment