Solved

echo implode (",", $array) question

Posted on 2011-09-17
5
475 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

752 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