<?php
function test($array)
{
try
{
return is_indexed_array($array);
}
catch (Exception $e)
{
echo $e->getMessage();
return 'error';
}
}
function is_indexed_array($array)
{
return !in_array(1, array_map('is_string', array_keys($array)));
}
//// 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']) === 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
- Output for git.master, git.master_jit, rfc.property-hooks
- Fatal error: Uncaught AssertionError: assert(test([1 => 'a', 2 => 'b', 3 => 'c']) === false) in /in/QIqsf:33
Stack trace:
#0 /in/QIqsf(33): assert(false, 'assert(test([1 ...')
#1 {main}
thrown in /in/QIqsf on line 33
Process exited with code 255.
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
51.18 ms | 401 KiB | 8 Q