SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question foreach all except last

    maybe someone know how to foreach all except one?

    Code:
    while($row = mysql_fetch_array($query)) {
    	$drink = $row['today_drink'];
    	$data[$drink][] = $row;
    }
    foreach ($data as $m => $news) {
    	echo "{$m}<br />";
    	if(end($data) === $news) {
    		echo "the last one is $m";
    	}
    }
    shows
    redbull
    shark
    batterythe last one is battery

    but i wan't
    redbull
    shark
    the last one is battery

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $end end($data);

    foreach (
    $data as $m => $news) {

            if (
    $end != $news)
                echo 
    "{$m}<br />";
            else 
            echo 
    "the last one is $m";
        

    I pulled end($data) out of the loop just for code efficiency.

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,889
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:
    define('js''&nbsp; &nbsp;');

    $data = array
    (
      
    'tom',
      
    'dick',
      
    'harry',
      
    'redbull',
      
    'shark',
      
    'battery',
      
    'nginx '
    );
        
        
    foreach (
    $data as $news => $m)
    {
        if( 
    $news count($data) )
        {
            echo 
    $newsjscount($data), js,js,"{$m}<br />";    
        }
        else
        {
            echo 
    "the last one is $m";
        }
    }

    die; 
    output:

    0 7 tom
    1 7 dick
    2 7 harry
    3 7 redbull
    4 7 shark
    5 7 battery
    the last one is nginx
    #not efficient but it works
    Last edited by John_Betong; Feb 10, 2012 at 12:08. Reason: forgot to include output:
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    #2 works..
    #3 too hard to understand but OK!

    thanks !

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    Try this:

    PHP Code:
    define('js''* *');

    $data = array
    (
      
    'tom',
      
    'dick',
      
    'harry',
      
    'redbull',
      
    'shark',
      
    'battery',
      
    'nginx '
    );
        
        
    foreach (
    $data as $news => $m)
    {
        if( 
    $news count($data) )
        {
            echo 
    $newsjscount($data), js,js,"{$m}<br />";    
        }
        else
        {
            echo 
    "the last one is $m";
        }
    }

    die; 
    output:


    #not efficient but it works
    Arrays in PHP aren't really arrays in the strictest sense of the term. What PHP calls arrays are called dictionaries in other languages. Your example falls apart because keys in PHP can be arbitrary. If they aren't defined (as in your example) they are 0 indexed like arrays in all other languages. But this loop wouldn't work with a result set from a database keyed by primary key - and it wouldn't work with any array with string keys.

    There are a couple of solutions. I consider this the most straightforward:

    Code php:
     
    // Detach the last element 
    $last = array_pop($data);
     
    // Loop the others.
    foreach ($data as $item) {
      echo $item."<br>";
    }
     
    // Special handling of last
    echo "And the last one is {$last}";
     
    // Reattach the last element.
    array_push($last);

    This works, but it's destructive to the key data on the last element. If that isn't acceptable this solution preserves the key data.

    Code php:
    $i = 1;
     
    foreach ($data as $item) {
      if ( $i != count($data) ) {
        echo $item."<br>";
      } else {
        echo "And the last one is ".$item";
      }
      $i++;
    }

    @wonshikee - Your example only works if the all the values are unique. It there is duplication between the last value and any of the previous both will be labeled as last.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •