Created
October 8, 2011 12:12
-
-
Save rudiedirkx/1272230 to your computer and use it in GitHub Desktop.
is_numeric_array
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 | |
$a1 = array('x', 'a' => 'b', 'c', 'd', 'e'); | |
print_r($a1); | |
var_dump(is_numeric_array($a1)); // false | |
$a2 = array('a', 'b', 'c', 'd', 'e'); | |
print_r($a2); | |
var_dump(is_numeric_array($a2)); // true | |
$a3 = array_fill(1, 6, 0) + array(0); | |
print_r($a3); | |
var_dump(is_numeric_array($a3)); // false, because keys start at 1 | |
$a4 = $a2; | |
unset($a4[2]); | |
print_r($a4); | |
var_dump(is_numeric_array($a4)); // false, because missing key in the middle | |
function is_numeric_array($arr) { | |
return $arr === array() || range(0, count($arr)-1) === array_keys($arr); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment