array_slice confused about negative length
Why is the second array below empty? It seems array_splice cannot grok negative length, despite its docs.
<?php
$array = array('a' => '1');
// GOOD
print_r(array_slice($array, 0, 10));
// WTF. Why empty array?
print_r(array_slice($array, 0, -10));
?>Array
(
[a] => 1
)
Array
(
)
Comments
It does work, but differently
As commenting was inoperative due to a site issue, I wrote my comment as a blog post.
In short, it does work as designed and specified, but it may not perhaps be what you would intuitively expect (i.e. counting back 10 elements starting at the start point). Instead, it counts forward from the start point, until 10 elements before the end point (which in this case lies well before the start point, even outside the array), which explains why the empty array is returned.
Post new comment