具有变量键名称的内联关联数组

时间:2012-11-13 05:52:15

标签: php arrays associative-array

我正在使用关联数组来构建相册列表以及这些相册中的图像。该数组使用以下代码构建(为清楚起见,省略了一些代码 - 仅加载$title$desc等的内容:)

<?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>';
    }
  }
?>

我正在尝试使用implode()根据$currentalbum(从Cookie中读取)吐出相关元素,如下所示:

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

这将返回错误:

  

警告:implode()[function.implode]:在第97行的index.php中传递的参数无效

我认为它对数组有一些问题,但当我print_r()时,我得到以下内容,看起来不错:

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>
)

知道我为什么会收到错误吗?

2 个答案:

答案 0 :(得分:2)

implode的第二个参数应该是一个数组。您的foreach循环似乎$photolist[$currentalbum]是一个字符串。

尝试

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

答案 1 :(得分:1)

我相信内爆需要一个角色作为第一个属性。尝试使用空格或逗号。

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

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