Echo'ing end of array

Discussion in 'PHP' started by cigi, Oct 24, 2009.

  1. #1
    Hi Guys.

    I'm having problems with the below code. When I try and echo the end of the array 'echo end($i);' I don't have anything echo'ed.

    I've tried test using:
    $i = array(2,6,4,3,56,3);
    echo end($i);

    This works I get '3' echo'ed.

    Can anyone see what I'm doing wrong?


    Many thanks



    
    <?php
    
    $next_p = $_GET['next_p'];
    	
    $filePath = $PATH .'../images/gallery';
    
    $dir = opendir($filePath);
    
    while ($file = readdir($dir)) { 
    
    if (eregi("\.jpg", $file)) { 
      
    $string  .= $file . ',';
    }}
    
    $i = explode(',', $string);
       
    echo end($i);
    
    ?>
    Code (markup):
     
    cigi, Oct 24, 2009 IP
  2. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    Trying print_r($i); First to make sure you aren't doing anything wrong with the array.
     
    Pudge1, Oct 24, 2009 IP
  3. DollaradeConner

    DollaradeConner Active Member Affiliate Manager

    Messages:
    200
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Try echoing $string just to make sure it has the correct string you want, it looks like it should work.
     
    DollaradeConner, Oct 24, 2009 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    
    $next_p = $_GET['next_p'];
    	
    $filePath = $PATH .'../images/gallery';
    
    $dir = opendir($filePath);
    
    while ($file = readdir($dir)) { 
    
    if (eregi("\.jpg", $file)) { 
      
    $string  .= $file . ',';
    }}
    $string = substr_replace($string ,"",-1);
    $i = explode(',', $string);
    
    echo end($i);
    
    ?>
    PHP:
    simple the last character of your string is "," so when u explode it last array will be empty so we remove last ",".

    :D:D
     
    xenon2010, Oct 24, 2009 IP
  5. tonythetiger

    tonythetiger Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Alternatively you could just do this...

    <?php
    
    $next_p = $_GET['next_p'];
       
    $filePath = $PATH .'../images/gallery';
    
    $dir = opendir($filePath);
    
    while ($file = readdir($dir)) {
    
    if (eregi("\.jpg", $file)) {
     
    $files[] = $file;
    }}
    
    $string = implode(',',$files);
    echo array_pop($files);
    
    ?>
    Code (markup):
    remember there is always more than one way to do things.
     
    tonythetiger, Oct 28, 2009 IP