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) { // your fav method here } //// 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)); // other assert(test([]) === true); // empty array assert(test(['' => '']) === true); // random test assert(test([0 => '']) === false); // random test assert(test([1 => 'a']) === true); // random test // 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 // string keys assert(test(['' => '', 'a' => 'a', 'b' => 'b']) === true); // proper associative array // mixed keys assert(test(['' => '', 1 => 'a', 2 => 'b']) === true); // first key string but rest not; also, associative even though '' == 0 assert(test([0 => '', 1 => 'a', 'b' => 'b']) === 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

preferences:
49.44 ms | 402 KiB | 5 Q