Monday, May 19, 2014

How To Limit Foreach Loop in PHP?

You can limit the foreach loop to any repetition. I needed to limit the foreach loop to two repetition.
I have used array_slice method inside foreach loop as following:
foreach(array_slice($articles, 0, 2) as $row ):
It was so easy.
The array_slice() function returns selected parts of an array.
Syntax
array_slice(array,start,length,preserve)

2 comments: