Ryann Green
Ryann Green

Reputation: 3

Calling an Array As a Value

I do not know if this is possible, but what I am trying to do is create a value for a key within an object that is an array, and then loop through that array in a for loop. My object is coded like this:

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

My code on my page looks like this:

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

I have called for the url change earlier on the page, and that is working correctly. I am positive my problem is in this section. This is probably obvious, but I am new to working with PHP, so any help, even if it's something really dumb that I've done, is greatly appreciated.

Upvotes: 0

Views: 38

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59601

Looks like you need a nested for-loop.

Here's the rough idea:

$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]'>";
        }
    }
}

Upvotes: 2

Related Questions