Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

echo implode (",", $array) question

Posted on 2011-09-17
5
Medium Priority
?
485 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to dynamically set the form action using jQuery.
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…

722 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