[UPHPU] array_slice offset inconsistency?

Jonathan Duncan jonathan at bluesunhosting.com
Sat Jan 30 21:38:53 MST 2010


Maybe it is just late, but this is bugging me.

I got this example from the PHP documentation (http://us3.php.net/manual/en/function.array-slice.php):

$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2);      // returns "c", "d", and "e"
print_r($output);
$output = array_slice($input, -2, 1);  // returns "d"
print_r($output);
$output = array_slice($input, 0, 3);   // returns "a", "b", and "c"
print_r($output);

What is baffling me is why the second array_slice returns "d" instead of "c", "d".

Perhaps the wording they use in the offset gives away the answer, but if so, then I submit that they are being inconsistent.  An array starts counting  at 0 (unless you specify otherwise).  If the non-negative offset specified is 2 then is will start at the third offset.  One would think that the negative offset should work the same way, but in reverse.

Someone set me straight, please.  I need to just put this down for the night.

Thanks,
Jonathan


More information about the UPHPU mailing list