Solved

echo implode (",", $array) question

Posted on 2011-09-17
5
481 Views
Last Modified: 2012-05-12
I have this little script to scan a directory of images. I want to write the image names into a javascript array.

I almost have it, but the problem is I get a trailing comma after the last item in the array.

So, is there a way to use implode instead of the way I did it?

<?PHP
  // filetypes to display
  $imagetypes = array("image/jpeg", "image/gif");
?>
    <?PHP
  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  function getImages($dir)
  {
    global $imagetypes;

    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // full server path to directory
    $fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";

    $d = @dir($fulldir) or die("getImages: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;

      // check for image files
      if(in_array(mime_content_type("$fulldir$entry"), $imagetypes)) {
        $retval[] = array(
         "file" => "/$dir$entry",
         "size" => getimagesize("$fulldir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }
?>
    <?PHP
  // fetch image details
  $images = getImages("webDesign");

  
  
  
  // display on page
  echo "<script>var imgArr = [";
  foreach($images as $img) {
    
    echo   "\"{$img['file']}\",\n"; // this is giving me a trailing comma
   
    //echo implode (",\n ", $images); // this is returning [Array, Array, Array...]
    }
  echo "]</script>";
?>

Open in new window

0
Comment
Question by:Kyle Hamilton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36555382
line 47 to 56 :


// display on page
  echo "<script>var imgArr = [";
  $first = true;
  foreach($images as $img) {
    if(!$first) echo ",";else $first=false;
    echo   "\"{$img['file']}\"\n"; // this is giving me a trailing comma
   
    //echo implode (",\n ", $images); // this is returning [Array, Array, Array...]
    }
  echo "]</script>";
?>

Open in new window

0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 36555386
Thanks again leakim971!

That did the trick.
0
 
LVL 3

Expert Comment

by:dkellner
ID: 36556460
If you have a comma at the end, use trim($yourString,",") for example.
Also consider using json_encode that transfers your array directly into a javascript object.
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 36556513
As always thanks for your help...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36556686
You're welcome, have a nice sunday and a good week!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question