How to access last value in foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

How to access last value in foreach loop

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Code: Select all

<?php

$array = array(1,2,3);

foreach ($array as $value) {
    if (!next($array)) {
        echo $value;
    }
}

// 3

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You may want to look into implode()
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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! :)
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post 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).
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Much beautiful is

Code: Select all

echo $array[count($array)-1];
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

and if assoc array? what do we do then?

And if empty numeric array? Undefined offset.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

what about end() ?
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Re: Reply

Post 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];
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Re: Reply

Post 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.
Post Reply