Jump to content

[SOLVED] if last loop??


Scooby08

Recommended Posts

ok.. I actually have that sorta thing going, but I'm still not getting how to tell if it's the last loop.. Here is sample code:

 

<?php
$amount = "one two three,two three four,three four five,four five six";
$pieces = explode(",",$amount);

$i = 1;
$data = array();
foreach($pieces as $piece){
$data = explode(" ",$piece);

if ($i == 1) {
} else if ($i == 2) {
} else {
	echo "<pre>";
	print_r($data);
	echo "</pre>";
}
$i++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/143323-solved-if-last-loop/#findComment-751699
Share on other sites

$cnt = count($peices);
foreach($pieces as $piece){
   $data = explode(" ",$piece);
   
   if ($i == 1) {
   } else if ($i == 2) {
   } elseif ($i == $cnt) {
         echo 'Last loop!';
   }else {
      echo "<pre>";
      print_r($data);
      echo "</pre>";
   }
   $i++;
}

Link to comment
https://forums.phpfreaks.com/topic/143323-solved-if-last-loop/#findComment-751702
Share on other sites

If numeric array...

 

foreach ($rows as $key => $row) {
   if (!$rows[$key+1]) {
      // this is last loop iteration, do something
   } // end if
} // end foreach

 

If associative array...

 

$count = count($rows) - 1;
foreach($rows as $row) {
   if ($count == 0) {
      // this is last loop iteration, do something
   } else {
      $count--;
   } // end if...else
} // end foreach

 

Link to comment
https://forums.phpfreaks.com/topic/143323-solved-if-last-loop/#findComment-751709
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.