#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    How to limit loop to all but last value in array??


    I have this code which works as expected:

    PHP Code:
    foreach($bidders as $val) { 
    $losing_bidder = ($val->email); 
    echo 
    $losing_bidder '<br>;

    Let us assume the output is:
    Bob@email.com
    Joe@email.com
    Cindy@email.com
    Willis@email.com

    I need to have the loop always ignore (or not echo, or not process) the last value in the array. What adjustment to the code would accommodate that??

    Thanks much!
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,264
    Rep Power
    2039
    The "easy" way to do this is...

    PHP Code:
    $winner array_pop ($bidders);

    foreach (
    $bidders as ... 
    Of course... That will only be useful if you don't need the entire array in the future. If you did need that, there's always...

    PHP Code:
    for ($i 0$i count ($bidders) - 1$i++) {
        
    $bidder $bidders [i];

    Comments on this post

    • dwest100 agrees
  4. #3
  5. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,109
    Rep Power
    2010
    He could also call array_push($winner) after the array_pop and loop.

    Comments on this post

    • dwest100 agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0
    Thank you kind sir! Pop works perfectly
    Much appreciated!

IMN logo majestic logo threadwatch logo seochat tools logo