3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test($array) { try { return is_associative_array($array); } catch (Exception $e) { echo $e->getMessage(); return 'error'; } } function is_associative_array(&$array) { $i = 0; foreach ($array as $k => $v) if ($k !== $i++) return true; return $i === 0; } //// non-array - adjust according to your preferred error handling technique //assert(test(null)); //assert(test('')); //assert(test(0)); //assert(test(false)); //assert(test(new stdClass)); // empty array assert(test([]) === true); // numeric keys assert(test([null, 'a', 'b']) === false); // proper indexed array; also, to catch wrong isset usage assert(test([1 => 'a', 2 => 'b', 3 => 'c']) === true); // sequential but not 0-based assert(test([-1 => '', 0 => '', 1 => 'a']) === true); // sequential but not 0-based assert(test([0 => '', 2 => 'b', 3 => 'c']) === true); // 1 index missing assert(test([0 => '', 2 => 'b', 1 => 'a']) === true); // numeric but unordered assert(test([0 => '']) === false); // single item assert(test([1 => 'a']) === true); // single item assert(test([NAN]) === false); assert(test([NAN => '']) === true); assert(test([INF]) === false); assert(test([INF => '']) === true); // string keys assert(test(['' => '', 'a' => 'a', 'b' => 'b']) === true); // proper associative array assert(test(['' => '']) === true); // single item; also, associative even though '' == 0 // mixed keys assert(test(['' => '', 1 => 'a']) === true); // first key string but rest not assert(test(['' => '', 0 => '']) === true); // first key string but rest not assert(test([0 => '', 'a' => 'a']) === true); // first key numeric but rest not // numeric string keys assert(test([' 0' => '']) === true); // associative even though ' 0' == 0 assert(test(['00' => '']) === true); // associative even though '00' == 0 assert(test(['0*' => '']) === true); // associative even though '0*' == 0 assert(test(['0.0' => '']) === true); // associative even though '0.0' == 0.0 assert(test(['0x0' => '']) === true); // associative even though '0x0' == 0 assert(test(['0e0' => '']) === true); // associative even though '0e0' == 0
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Implicit conversion from float NAN to int loses precision in /in/IfdRJ on line 45 Fatal error: Uncaught AssertionError: assert(test([NAN => '']) === true) in /in/IfdRJ:45 Stack trace: #0 /in/IfdRJ(45): assert(false, 'assert(test([NA...') #1 {main} thrown in /in/IfdRJ on line 45
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Fatal error: Uncaught AssertionError: assert(test([NAN => '']) === true) in /in/IfdRJ:45 Stack trace: #0 /in/IfdRJ(45): assert(false, 'assert(test([NA...') #1 {main} thrown in /in/IfdRJ on line 45
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Warning: assert(): assert(test([NAN => '']) === true) failed in /in/IfdRJ on line 45 Warning: assert(): assert(test([INF => '']) === true) failed in /in/IfdRJ on line 47
Output for 5.5.0 - 5.5.37, 5.6.0 - 5.6.28

preferences:
211.06 ms | 402 KiB | 221 Q