3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function test($array) { try { $array = (object)$array; return is_indexed_array($array); } catch (Exception $e) { echo $e->getMessage(); return 'error'; } } assert(test([]) === true); assert(test(['', 'a', 'b']) === true); // proper indexed array; also, to catch wrong isset usage assert(test([1 => 'a', 2 => 'b', 3 => 'c']) === false); // sequential but not 0-based assert(test([-1 => '', 0 => '', 1 => 'a']) === false); // sequential but not 0-based assert(test([0 => '', 2 => 'b', 3 => 'c']) === false); // 1 index missing assert(test([0 => '', 2 => 'b', 1 => 'a']) === false); // numeric but unordered assert(test([0 => '']) === true); // single item assert(test([1 => 'a']) === false); // single item assert(test([NAN]) === true); assert(test([NAN => '']) === false); assert(test([INF]) === true); assert(test([INF => '']) === false); assert(test([' ' => '', 'a' => 'a', 'b' => 'b']) === false); // proper associative array assert(test([' ' => '']) === false); // single item; also, associative even though '' == 0 assert(test([' ' => '', 1 => 'a']) === false); // first key string but rest not assert(test([' ' => '', 0 => '']) === false); // first key string but rest not assert(test([0 => '', 'a' => 'a']) === false); // first key numeric but rest not assert(test([' 0' => '']) === false); // associative even though ' 0' == 0 assert(test(['00' => '']) === false); // associative even though '00' == 0 assert(test(['0*' => '']) === false); // associative even though '0*' == 0 assert(test(['0.0' => '']) === false); // associative even though '0.0' == 0.0 assert(test(['0x0' => '']) === false); // associative even though '0x0' == 0 assert(test(['0e0' => '']) === false); // associative even though '0e0' == 0 assert(test(['-0' => '']) === false); // associative even though '0e0' == 0 $obj = new stdClass; $obj->{0} = ''; assert(test((array)$obj) === false); // to catch ['0' => ''] like situations function is_indexed_array($array) { foreach (array_keys($array) as $k => $v) if ($k !== $v) return false; return true; }
Output for 7.0.0 - 7.2.0
Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([1 => 'a', 2 => 'b', 3 => 'c']) === false) failed in /in/f7Ma0 on line 21 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([-1 => '', 0 => '', 1 => 'a']) === false) failed in /in/f7Ma0 on line 22 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([0 => '', 2 => 'b', 3 => 'c']) === false) failed in /in/f7Ma0 on line 23 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([0 => '', 2 => 'b', 1 => 'a']) === false) failed in /in/f7Ma0 on line 24 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([1 => 'a']) === false) failed in /in/f7Ma0 on line 26 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([NAN => '']) === false) failed in /in/f7Ma0 on line 28 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([INF => '']) === false) failed in /in/f7Ma0 on line 30 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([' ' => '', 'a' => 'a', 'b' => 'b']) === false) failed in /in/f7Ma0 on line 31 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([' ' => '']) === false) failed in /in/f7Ma0 on line 32 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([' ' => '', 1 => 'a']) === false) failed in /in/f7Ma0 on line 33 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([' ' => '', 0 => '']) === false) failed in /in/f7Ma0 on line 34 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([0 => '', 'a' => 'a']) === false) failed in /in/f7Ma0 on line 35 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test([' 0' => '']) === false) failed in /in/f7Ma0 on line 36 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test(['00' => '']) === false) failed in /in/f7Ma0 on line 37 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test(['0*' => '']) === false) failed in /in/f7Ma0 on line 38 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test(['0.0' => '']) === false) failed in /in/f7Ma0 on line 39 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test(['0x0' => '']) === false) failed in /in/f7Ma0 on line 40 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test(['0e0' => '']) === false) failed in /in/f7Ma0 on line 41 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test(['-0' => '']) === false) failed in /in/f7Ma0 on line 42 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): assert(test((array)$obj) === false) failed in /in/f7Ma0 on line 43
Output for 5.4.10 - 5.6.28
Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 21 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 22 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 23 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 24 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 26 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 28 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 30 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 31 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 32 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 33 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 34 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 35 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 36 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 37 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 38 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 39 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 40 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 41 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 42 Warning: array_keys() expects parameter 1 to be array, object given in /in/f7Ma0 on line 49 Warning: Invalid argument supplied for foreach() in /in/f7Ma0 on line 49 Warning: assert(): Assertion failed in /in/f7Ma0 on line 43
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/f7Ma0 on line 19
Process exited with code 255.