NaOH
NaOH

Reputation: 459

Implode associative array with variable key name

I'm using an associative array to build a list of albums and the images inside those albums. The array is built with the following code (some code omitted for clarity -- just the stuff that loads $title, $desc, etc.):

<?php               
  $directory = 'gallery/*';
  $subfolders = glob($directory);
  foreach($subfolders as $subfolder) {
    $photos = glob($subfolder.'/*.[Jj][Pp][Gg]');
    $album = explode('_', $subfolder);
    $albumname = str_replace(' ','%20',$album[1]);
    foreach($photos as $photo){
      $photolist[$albumname] .= '<span data-title="'.$title.'" data-desc="'.$desc.'" data-camera="'.$camera.'" data-lens="'.$lens.'" data-length="'.$length.'" data-shutter="'.$shutter.'" data-aperture="'.$aperture.'" data-iso="'.$iso.'" style="background-image:url('.$photo.'); background-size:contain;" class="slide"></span>';
    }
  }
?>

I'm trying to use implode() to spit out the relevant elements based on the $currentalbum (read from a cookie) as follows:

<?php
  if(isset($_COOKIE["currentalbum"])) {
    $currentalbum = $_COOKIE["currentalbum"];
  } else {
    $currentalbum = "New";
  }
  $currentphotolist = implode("",$photolist[$currentalbum]);
  echo $currentphotolist;
?>

This is returning the error:

Warning: implode() [function.implode]: Invalid arguments passed in index.php on line 97

I presume it has some problem with the array, but when I print_r() it, I get the following, which looks fine:

Array
(
[New] => <span data-title="Train" data-desc="This is a picture of a train.  Look at it go!" data-camera="Nikon D300" data-lens="Tamron 17-50mm f/2.8 VC" data-length="17mm" data-shutter="1/250s" data-aperture="f/6.3" data-iso="200" style="background-image:url(gallery/01_New/Train.jpg); background-size:contain;" class="slide"></span><span data-title="Billow" data-desc="" data-camera="Nikon D300" data-lens="Nikkor 50mm f/1.8D" data-length="50mm" data-shutter="1/250s" data-aperture="f/8" data-iso="200" style="background-image:url(gallery/01_New/Billow.jpg); background-size:contain;" class="slide"></span><span data-title="3059" data-desc="" data-camera="Nikon D300" data-lens="Micro-Nikkor 105mm f/2.8 VR" data-length="105mm" data-shutter="1/30s" data-aperture="f/22" data-iso="200" style="background-image:url(gallery/01_New/3059.jpg); background-size:contain;" class="slide"></span>
[Landscapes] => <span data-title="Influx" data-desc="" data-camera="Nikon D300" data-lens="Nikkor 70-300mm f/4.5-5.6 VR" data-length="70mm" data-shutter="30s" data-aperture="f/8" data-iso="200" style="background-image:url(gallery/02_Landscapes/Influx.jpg); background-size:contain;" class="slide"></span>
[Constructs] => <span data-title="Fervor" data-desc="I took this while wandering around in SEA-TAC airport waiting for a flight home.  It\'s a lantern, hanging with some others in a shop.  It caught my eye from across the terminal, but the shop was pretty small and I\'m always a little worried about knocking stuff with my camera bag, so I shot it through the window.  I think it turned out pretty well regardless." data-camera="Nikon D7000" data-lens="Tamron 17-50mm f/2.8 VC" data-length="50mm" data-shutter="1/60s" data-aperture="f/2.8" data-iso="180" style="background-image:url(gallery/03_Constructs/Fervor.jpg); background-size:contain;" class="slide"></span>
)

Any idea why I'm getting the error?

Upvotes: 0

Views: 679

Answers (2)

air4x
air4x

Reputation: 5683

The second argument to implode should be an array. It seems from your foreach loop that $photolist[$currentalbum] is a string.

Try

foreach($photos as $photo){
  $photolist[$albumname][]= '<span data-title...</span>';
}

Upvotes: 2

Pastor Bones
Pastor Bones

Reputation: 7351

I believe implode requires a character as the first attribute. Try using a space or comma.

// Space
$currentphotolist = implode(" ",$photolist[$currentalbum]);

// Comma
$currentphotolist = implode(",",$photolist[$currentalbum]);

Upvotes: 1

Related Questions