Page 1 of 1
How to access last value in foreach loop
Posted: Mon Aug 13, 2007 1:54 am
by dream2rule
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
Posted: Mon Aug 13, 2007 2:37 am
by jmut
Code: Select all
<?php
$array = array(1,2,3);
foreach ($array as $value) {
if (!next($array)) {
echo $value;
}
}
// 3
?>
Posted: Mon Aug 13, 2007 2:39 am
by Benjamin
You may want to look into
implode()
Posted: Mon Aug 13, 2007 3:46 am
by dream2rule
jmut wrote:Code: Select all
<?php
$array = array(1,2,3);
foreach ($array as $value) {
if (!next($array)) {
echo $value;
}
}
// 3
?>
Thanks, this worked!

Reply
Posted: Mon Aug 13, 2007 7:38 am
by user___
Hi guys,
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 value
This is a cleaner and much faster solution(three times faster).
Posted: Mon Aug 13, 2007 8:07 am
by miro_igov
Posted: Mon Aug 13, 2007 8:34 am
by jmut
and if assoc array? what do we do then?
And if empty numeric array? Undefined offset.
Posted: Mon Aug 13, 2007 8:40 am
by feyd
Posted: Mon Aug 13, 2007 8:54 am
by Zoxive
Reply
Posted: Mon Aug 13, 2007 9:59 am
by user___
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).
Code: Select all
for($i = 0; $i < round(($result/$div)+3); $i++) {
}//Bad because the interpreter needs to execute that round too many times
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.
Re: Reply
Posted: Mon Aug 13, 2007 10:08 am
by CoderGoblin
user___ 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
You may also want to look at
foreach
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.
I tend to dislike array_pop as it modifies the array if I need to use the array again. In this instance I would use
Code: Select all
$lastkey=array_pop(array_keys($array));
$last_array_value=$array[$lastkey];
Reply
Posted: Mon Aug 13, 2007 10:14 am
by user___
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.
Posted: Mon Aug 13, 2007 10:20 am
by jmut
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.
Re: Reply
Posted: Mon Aug 13, 2007 10:28 am
by CoderGoblin
user___ wrote:...but about that useless looping through the array I am afraid I can not agree with that....
The question you need to ask yourself is simply ...if you need only the last value why get the full array ?
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.