@ 2015-11-23T18:35:44Z <?php
function test($array)
{
try
{
return is_indexed_array($array);
}
catch (Exception $e)
{
echo $e->getMessage();
return 'error';
}
}
// non-array - adjust according to your preferred error handling technique
assert(test(null) === false);
assert(test(false) === false);
assert(test(new stdClass) === false);
// empty array
assert(test([]) === true);
// numeric keys
assert(test([null, '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);
// string keys
assert(test(['' => '', 'a' => 'a', 'b' => 'b']) === false); // proper associative array
assert(test(['' => '']) === false); // single item; also, associative even though '' == 0
// mixed keys
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
// numeric string keys
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
$obj = new stdClass; $obj->{0} = '';
assert(test((array)$obj) === false); // to catch ['0' => ''] like situations
function is_indexed_array(&$array)
{
if (!is_array($array))
return false;
$i = -1;
foreach ($array as $k => $v)
if ($k !== ++$i)
return false;
return $i === -1;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.27 , 8.2.0 - 8.2.17 , 8.3.0 - 8.3.4 Fatal error: Uncaught AssertionError: assert(test([null, 'a', 'b']) === true) in /in/iZIm9:29
Stack trace:
#0 /in/iZIm9(29): assert(false, 'assert(test([nu...')
#1 {main}
thrown in /in/iZIm9 on line 29
Process exited with code 255 . Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 Warning: assert(): assert(test([null, 'a', 'b']) === true) failed in /in/iZIm9 on line 29
Warning: assert(): assert(test([0 => '']) === true) failed in /in/iZIm9 on line 34
Warning: assert(): assert(test([NAN]) === true) failed in /in/iZIm9 on line 36
Warning: assert(): assert(test([INF]) === true) failed in /in/iZIm9 on line 38
Output for 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 Warning: assert(): Assertion failed in /in/iZIm9 on line 29
Warning: assert(): Assertion failed in /in/iZIm9 on line 34
Warning: assert(): Assertion failed in /in/iZIm9 on line 36
Warning: assert(): Assertion failed in /in/iZIm9 on line 38
preferences:dark mode live preview
159.6 ms | 402 KiB | 209 Q