将数组称为值

时间:2014-08-14 04:06:17

标签: php arrays for-loop foreach

我不知道这是否可行,但我想要做的是为一个数组对象中的一个键创建一个值,然后在for循环中循环遍历该数组。我的对象编码如下:

<?php $shoots[01] = array(
"name" => "Rustic Farm Shoot",
"id" => "rustic",
"img" => array("img/slider.jpg", "img/testb2.jpg")
);  ?>

我页面上的代码如下所示:

 <div id="main-content">
   <div class="slideshow">
    <?php foreach($shoots as $shootID => $shoot) { 
      if($_GET["id"] == $shootID) { 
        for (i = 0; i < $shoot['img'[]].length ; i++) { ?>
          <img src="<?php echo $shoot['img'[i]]; ?>" alt="<?php echo $shoot['name']; ?>">
    <?php }}}; ?>
  </div>
 </div>

我已经在网页上提前更改了网址,这是正常的。我很肯定我的问题在于本节。这可能是显而易见的,但我是使用PHP的新手,所以任何帮助,即使它是我所做的非常愚蠢的事情,我们都非常感激。

1 个答案:

答案 0 :(得分:2)

看起来你需要一个嵌套的for循环。

这是一个粗略的想法:

$shoots[01] = array(
    "name" => "Rustic Farm Shoot",
    "id" => "rustic",
    "img" => array("img/slider.jpg", "img/testb2.jpg")
);

foreach($shoots as $shootID => $shoot) { 
    if($_GET["id"] == $shootID) { 
        foreach($shoot['img'] as $img) {
            echo "<img src='$img' alt='$shoot[name]'>";
        }
    }
}