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
Post a Comment