php - Getting rid of comma on last element of array -


i have array loops through music tracks need remove comma last element of array, can help?

<?php foreach ($bucket_contents $file){ $fname = $file['name']; $furl = "http://".$isdhtml5bucket.".s3.amazonaws.com/".urlencode($fname);  if(preg_match("/\.mp3$/i", $furl)) { ?>     {             name:"<?php echo basename($fname); ?>",             mp3:"<?php echo $furl; ?>"          }, <?php }else{ ?>         {             name:"<?php echo basename($fname); ?>",             m4a:"<?php echo $furl; ?>"          },<?php } } ?> 

and here output of array.

    {         name:"bassline",         m4a:"http://suyssamuelarawaeast.s3.amazonaws.com/warehouse%2fbassline%2f"      },     {         name:"project bassline - drop pressure (jack beats rinsed out remix).mp3",         mp3:"http://suyssamuelarawaeast.s3.amazonaws.com/warehouse%2fbassline%2fproject+bassline+-+drop+the+pressure+%28jack+beats+rinsed+out+remix%29.mp3"      },         {         name:"deadmau5 vs jelo   reward cheese.mp3",         mp3:"http://suyssamuelarawaeast.s3.amazonaws.com/warehouse%2fdeadmau5+vs+jelo+++the+reward+is+cheese.mp3"      },     {         name:"vocal",         m4a:"http://suyssamuelarawaeast.s3.amazonaws.com/warehouse%2fvocal%2f"      },                   {         name:"thin ice",         free:true,         mp3:"http://www.jplayer.org/audio/mp3/miaow-10-thin-ice.mp3",         oga:"http://www.jplayer.org/audio/ogg/miaow-10-thin-ice.ogg"     }, 

if scroll last element of array can see comma output. need remove comma last element of array.

thanks

pass string through rtrim() , specify comma in charlist removable character:

$str = rtrim($str, ','); 

edit

or... perhaps approach (not tested):

$songs = array();  foreach ($bucket_contents $file) {       $fname = $file['name'];     $furl  = "http://".$isdhtml5bucket.".s3.amazonaws.com/".urlencode($fname);      $type = preg_match("/\.mp3$/i", $furl) ? 'mp3' : 'm4a';      $songs[] = sprintf('{ name: %s, %s: %s }', $fname, $type, $furi);  }  echo implode(',', $songs); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -