Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
is_numeric_array
<?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
You can’t perform that action at this time.