If last key in a array

Discussion in 'PHP' started by blueparukia, Apr 5, 2008.

  1. #1
    I have an array, and I want sort through and echo the results using foreach.

    However, on the last key of the array, I want to put a period at the end of the key before it is echoed.

    How would I do this?
     
    blueparukia, Apr 5, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You could do something like:
    
    foreach ($array as $id => $item){
    // ...
        if ($id == (count($array)-1)){
           // Add your period
        }
    }
    
    PHP:
    This should guide you a bit, hopefully.

    Jay
     
    jayshah, Apr 5, 2008 IP
    blueparukia likes this.
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Thanks, it works :D

    The only downside is it goes like:

    
    $array = array(
    1 => "value1",
    2 => "value2",
    3 => "value3",
    );
    PHP:
    I'd rather use "key1" => "value1", but it is no biggie - it works anyway :D :D

    Thanks,

    BP
    ?>[/php]
     
    blueparukia, Apr 5, 2008 IP