Alex Bowers wrote:

> Anywhere on the front-end where a foreach() is used, and expects at most
> say, 10 items. But the full dataset is fetched back (to show a summary
> after the first 10 or whatever other reason).
> 
> The old code would have required a counter, the new code does not. This
> would make it cleaner for the front-end developer to understand.
> 
> <?php
> $results = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
> 
> // Old
> $count = 0;
> 
> foreach($results as $result){
> if($count++ > 9) break;
> echo $result . "\n"; // 1 2 3 4 5 6 7 8 9
> }
> 
> // New
> 
> foreach($results[*:9] as $result) {
> echo $result . "\n"; // 1 2 3 4 5 6 7 8 9
> }

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

-- 
Christoph M. Becker

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to