Echo'ing end of array

Resna
Resna used Ask the Experts™
on
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

Resna
<?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);
 
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
are you sure $string is being built correctly?

try

echo $string; // before the expode.

maybe

if (eregi("\.jpg", $file)) {

should be

if (eregi("/.jpg", $file)) {

Author

Commented:
Thanks Michael701

If I echo $string I get:

0.jpg,1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,

Which is the file name in the directory being opened.

Many thanks

Resna
Ah ha, that's why end($i) returns nothing, the last ,

$i = explode(',', $string);

to

$i = explode(',', substr($string,0,-1));

Author

Commented:
Oh yes that makes sence.

And it works YAY.

Thanks Michael701

Resna

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial