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):
Try echoing $string just to make sure it has the correct string you want, it looks like it should work.
<?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 ",".
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.