Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Created October 8, 2011 12:12
Show Gist options
  • Save rudiedirkx/1272230 to your computer and use it in GitHub Desktop.
Save rudiedirkx/1272230 to your computer and use it in GitHub Desktop.
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