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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.