2

How can I get the last element of an array without changing its internal pointer?

What i'm doing is:

while(list($key, $value) = each($array)) {  
    //do stuff  
    if($condition) {  
        //Here I want to check if this is the last element of the array            
        prev($array);  
    }  
}

so end($array) would mess things up.

8
  • 4
    Hint: array_keys. Also, click the big orange question mark to get formatting help and instructions. Commented Dec 15, 2012 at 9:44
  • @PankitKapadia end() changes the arrays internal pointer, and it's mentioned in the question that it isn't of any help. Commented Dec 15, 2012 at 9:54
  • use $count = count($array); and check using it. Commented Dec 15, 2012 at 9:55
  • @xyu - check the answer. is it what you want? :-/ Commented Dec 15, 2012 at 10:01
  • Does that array may contain associative keys (keys with not just integers but also strings)? Commented Dec 15, 2012 at 10:02

4 Answers 4

2

Try this:

<?php
$array=array(1,2,3,4,5);
$totalelements = count($array);
$count=1;

while(list($key, $value) = each($array)) {  
    //do stuff  
    if($count == $totalelements){ //check here if it is last element
        echo $value;
    }
    $count++;
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Can be done like for($count=1;list($key, $value) = each($array));$count++)
1

Warning: this accepted answer is outdated and now triggers the following error. The PHP manual for the end() specifically says that function's parameter is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Notice: Only variables should be passed by reference

It's simple, you could use:

$lastElementKey = end(array_keys($array));
while(list($key, $value) = each($array)) {  
    //do stuff  
    if($key == $lastElementKey) {  
        //Here I want to check if this is the last element of the array            
        prev($array);  
    }  
}

6 Comments

I ended up using the solution in the answer of this question
Not sure how that answer applies but ok
The answer provided in that other question wont work for you if your array contains the same value more than once. The correct way is to check the keys and not the values.
My solution uses no if's so I suppose that it is faster.
It does the job (the array has distinct values).
|
1

Why not just using something like:

$lastElement= end($array);
reset($array);
while(list($key, $value) = each($array)) {  
    //do stuff   
}

// Do the extra stuff for the last element

5 Comments

not a good solution if the first and last elements are the same. The correct solution is to use the array keys.
I wanted to check if $value is the last element.
@xyu ? Why doesn't it work? From what I understood, you wanted to check it to do something else with it, right? You want to do the same thing for all elements except the last one where you want to do some extra stuff
It does. I never said it doesn't.
Oh... ok. I thought that you were writing like that because it wasn't a solution for the problem you stated.
0

Something like this:

$array = array_reverse($array, true);
$l = each($array);
$lastKey = $l['key'];
$array = array_reverse($array, true);

while(list($key, $value) = each($array)) {  
    //do stuff  
    if($key == $lastKey) {  
        echo $key . ' ' . $value . PHP_EOL;
    }  
}

The problem here is that if the array is big one then it'll take some time to reverse it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.