>
> // alternative old
>   foreach(array_slice($results, 0, 9) as $result) {
>       echo $result . "\n"; // 1 2 3 4 5 6 7 8 9
>   }
> Not so bad, in my opinion.


To be the same, your example would have to be:

// alternative old
  foreach(array_slice($results, 0, 9, true) as $result) {
      echo $result . "\n"; // 1 2 3 4 5 6 7 8 9
  }

since this will preserve the array keys.

This was a quick off the top of my head example; there is also the benefit
of overwriting multiple array items with a single line

$array[*1:4] = [1,2,3,4]; // Sets array items 1 through 4 to be the values
(and keys if provided), of the array assigned.

$array[*1:] = [1,2,3,4]; // Sets array items 1 to the end to be values (and
keys if provided), of the array assigned. This will also wipe out any
additional information to the end of the array. Keeping anything before the
first item, but extending until the end.

This would be similar to array_replace, however that works from keys, not
positions.

Reply via email to