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, 010));

// 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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options