How to access last value in foreach loop
Moderator: General Moderators
-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
How to access last value in foreach loop
Hello All,
I have this foreach loop which displays the entire data in a comma separated format.
I want to access the last value in the foreach loop as i don't need a comma to be followed for the last array element .
Regards
I have this foreach loop which displays the entire data in a comma separated format.
I want to access the last value in the foreach loop as i don't need a comma to be followed for the last array element .
Regards
Code: Select all
<?php
$array = array(1,2,3);
foreach ($array as $value) {
if (!next($array)) {
echo $value;
}
}
// 3
?>-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
Thanks, this worked!jmut wrote:Code: Select all
<?php $array = array(1,2,3); foreach ($array as $value) { if (!next($array)) { echo $value; } } // 3 ?>
Reply
Hi guys,
I have read all the suggestions so far and although they are cool there is a better one.
This is a cleaner and much faster solution(three times faster).
I have read all the suggestions so far and although they are cool there is a better one.
Code: Select all
$id = count($array)-1;
echo $array[$id];//Write these lines when you have your array and need to get the last valueMuch beautiful is
Code: Select all
echo $array[count($array)-1];Reply
feyd:great;
Zoxive:great;
miro_igov:Well, I am not sure about that. This has to do with coding habits. Imagine a situation like this(I do not say that this has something do with the one here but it is just about habits).
And imagine if that guy will need that value at a later time(One more execution of count() which is not the best solution.
I have tested all the suggested ways here and here are the results:
Mine:execution time 0.125 seconds
feyd:execution time 0.127 seconds
Zoxive:execution time 0.125 seconds
NOTICE:I do not say that these values are exact(If you reload the page you will see different values but in the range of 10-30(the last numbers)).
BTW:If I am faith I created that in your way initially but I fixed it later.
Zoxive:great;
miro_igov:Well, I am not sure about that. This has to do with coding habits. Imagine a situation like this(I do not say that this has something do with the one here but it is just about habits).
Code: Select all
for($i = 0; $i < round(($result/$div)+3); $i++) {
}//Bad because the interpreter needs to execute that round too many timesI have tested all the suggested ways here and here are the results:
Mine:execution time 0.125 seconds
feyd:execution time 0.127 seconds
Zoxive:execution time 0.125 seconds
NOTICE:I do not say that these values are exact(If you reload the page you will see different values but in the range of 10-30(the last numbers)).
BTW:If I am faith I created that in your way initially but I fixed it later.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Re: Reply
You may also want to look at foreachuser___ wrote:Code: Select all
for($i = 0; $i < round(($result/$div)+3); $i++) { }//Bad because the interpreter needs to execute that round too many times
If you need to step though the array once before you ever need the last value simply
Code: Select all
foreach ($array as $key=>$value) {
//process whatever
$last_array_key=$key;
}
// use $last_array_key wherever you need need it.Code: Select all
$lastkey=array_pop(array_keys($array));
$last_array_value=$array[$lastkey];Reply
Sorry, it seems that I tend to think about one thing and do another one. So I meant foreach not for(But it is not such a big deal). About the functions provided by feyd and Zoxive I can agree to some extent(It is up to you what you decide to do) but about that useless looping through the array I am afraid I can not agree with that. You should do some tests and you will see what I mean(Your version is seven time slower and a little more complicated than the one I posted).
Note:I do not want to argue but these versions seem to be not so fast and productive as they should be.
Note:I do not want to argue but these versions seem to be not so fast and productive as they should be.
it all depends. I wrote my response as I though more stuff is going on within this foreach than simply getting last value.
Meaning itteration is necessary any how...and just getting last itteration is important.
So everybody is discussing stuff and don't know *exactly* what the requirement is. Tons of ways to achieve. Nice input from everyone.
Meaning itteration is necessary any how...and just getting last itteration is important.
So everybody is discussing stuff and don't know *exactly* what the requirement is. Tons of ways to achieve. Nice input from everyone.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Re: Reply
The question you need to ask yourself is simply ...if you need only the last value why get the full array ?user___ wrote:...but about that useless looping through the array I am afraid I can not agree with that....
If you are processing the array somehow anyway before you need the last item simply set it as you process it. With a bit of jiggling with code order you should be able to do whatever you need to with the full array before you need the last item anyway.