Jump to content

[SOLVED] finding last item in a foreach()


law

Recommended Posts

According to a comment in the manual this should work:

<?php
$array = array('apples','bananas','cranberries','durians');
$last_item = end($array);

foreach($array as $item) {
  if ($item == $last_item) {
    print "I like to eat " . $item;
  }
}
?> 

source : http://us3.php.net/manual/en/control-structures.foreach.php comment by grobemo

 

here is my code:

foreach ($pieces as &$p2){
$last_piece = end($p2);
if($p2 = $last_piece && !isset($error)) {
         $error = "No Error";
}
}

Here is my error message: "Warning: end() [function.end]: Passed variable is not an array or object in C:\wamp\www\index.php on line 2 (in my example)"

 

Thanks guys I have been posting on the board all day. You guys have been a savior helping me meet this deadline.

Link to comment
Share on other sites

Try following the example from php.ner. -_-  You're calling end() on each individual element in the array, instead of once on the whole array, before the loop.

law did it correctly. He/She just didn't need to loop.

 

foreach ($pieces as &$p2){
   $last_piece = end($p2);

$pieces is the array, $p2 is the individual element.  What's wrong with that picture?

 

Also:

if ($p2 = last_piece ...

Should be 2 =.

Link to comment
Share on other sites

Ha! I'm stupid. I was looking at the first example. Don't mind me.

 

Sorry all.

 

UGH! and I just learned that $p2 was the individual elements of the array earlier today! Apparently my brain is on vacation.

 

 
$last_piece = end($pieces);
//echo $last_piece;
foreach ($pieces as &$p2){
// lots of unimportant code here
if($p2 == $last_piece && !isset($error)) {
$error = "No Error";
}						
}				

 

Works like a charm! Thanks again

Link to comment
Share on other sites

That seems redundant. Like I said, why not just skip the for loop and echo $last_piece?

 

I am trying to identify the last pass through of my foreach loop. I am doing a lot of other things utilizing the loop. I have stripped all of that out as it has nothing to do with this question. I am only using the $last_item to identify the last item in the array. The if() then checks to make sure in my code, that is not included, that there were no identified errors in the loop. As each time the loop runs it does many things which might have resulted in a error due to the way the user inputted the string I am parsing. This whole thing prevents the same error from being printed out to the user more than one time. With out the if() it would show the error every time the error was encountered.

 

Hope that made some sense? I commented where the missing code would have been in my previous post if that helps visualize what I am talking/typing about.

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.