3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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
based on V3bcV
Output for 7.0.0 - 7.2.0
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.6.28