Solved

echo implode (",", $array) question

Posted on 2011-09-17
5
465 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
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
Thanks again leakim971!

That did the trick.
0
 
LVL 3

Expert Comment

by:dkellner
Comment Utility
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
Comment Utility
As always thanks for your help...
0
 
LVL 82

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now