php - in foreach, isLastItem() exists? -


using regular loop, it's possible comapred current index last tell if i'm in last iteration of loop. there similar thing when using foreach? mean this.

foreach($array $item){   //do stuff   //then check if we're in last iteration of loop   $last_iteration = islast(); //boolean true/false } 

if not, there @ least way know current index of current iteration $iteration = 5, can manually compare length of $array?

you can use combination of spl’s arrayiterator , cachingiterator class have hasnext method:

$iter = new cachingiterator(new arrayiterator($arr)); foreach ($iter $value) {     $last_iteration = !$iter->hasnext(); } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -