0

I have tried to echo out an image from an array, but it did not work.

$productArr = [
    "PT" => [
        "cat" => [ "image" => "cat.jpg", "desc" => "blah blah"],
        "fish"=> [ "image" => "fish.jpg", "desc" => "blah blah"],
        "dog" => [ "image" => "dog.jpg","desc" => "blah blah"],
    ],
    "KC" => [
        "Ice" => [ "image" =>   "ice.jpg", "desc" => "mah mah mah"],
        "cold"=> [ "image" => "cold.jpg", "desc" => "mah mah mah"],
        "water"=> [ "image" =>   "water.jpg", "desc" => "mah mah mah"],
    ],

];

$featuredArr = [
    "KC" => "Ice",
    "PT" => "cat",
];

foreach ($featuredArr as $key => $value) {
   if ($productArr[$key][$value]) {
      echo "key $key=>$value exists:  <br />";
      foreach ($productArr[$key][$value] as $newKey => $newValue) {
         echo "$newKey['image']<br />";
      }
   }
}

I also want it to echo out Both "KC" and "PT" from $featuredArr. currently only "Cat" is being outputted.

3
  • You have a syntax error. echo "$newKey['image']<br />"; should be echo "{$newKey['image']}<br />";. How is it running at all? Commented Jan 2, 2015 at 12:14
  • Illegal string offset 'image' Commented Jan 2, 2015 at 12:19
  • That's because $newKey is a string, not an array. Commented Jan 2, 2015 at 12:20

4 Answers 4

1

$productArr[$key][$value] is already a 1-D array, for KC => Ice it holds the value:

[ "image" =>   "ice.jpg", "desc" => "mah mah mah"]

You have to simply output

echo $productArr[$key][$value]['image'], "<br />";

Also, you should use isset() or empty() to check whether to element exists such as:

if (isset($productArr[$key]) && isset($productArr[$key][$value]))

this prevents undefined index errors.

Sign up to request clarification or add additional context in comments.

Comments

0

The inner foreach loop is unneccesary and doing strange things. Try running this, it seems to work:

$productArr = [
    "PT" => [
        "cat" => [ "image" => "cat.jpg", "desc" => "blah blah"],
        "fish"=> [ "image" => "fish.jpg", "desc" => "blah blah"],
        "dog" => [ "image" => "dog.jpg","desc" => "blah blah"],
    ],
    "KC" => [
        "Ice" => [ "image" =>   "ice.jpg", "desc" => "mah mah mah" ],
        "cold"=> [ "image" => "cold.jpg", "desc" => "mah mah mah" ],
        "water"=> [ "image" =>   "water.jpg", "desc" => "mah mah mah" ],
    ],

];

$featuredArr = [
    "KC" => "Ice",
    "PT" => "cat",
];

foreach ($featuredArr as $key => $value) {
   if ($productArr[$key][$value]) {
      echo "key $key=>$value exists:  <br />";
      echo $productArr[$key][$value]['image'] . '<br />';
   }
}

You can quickly test stuff like this using a PHP repl site, like this one:

http://phpepl.herokuapp.com/

3 Comments

in my system, it only produced key PT=>cat exists: cat.jpg
Sounds like a problem on your system then, do you have all error reporting active? It might be there is some sort of problem your system is supressing. Does it work when you run this code on the REPL I linked?
i found the reason why, i cannot have the same key value in my $featuredArr
0

Not exactly to your question but I think my slider example may help you, please keep few image with name image1.jpg image2.jpg.... in same folder

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style>
#cycler{position:relative;}
#cycler img{position:absolute;z-index:1}
#cycler img.active{z-index:3}
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>

function cycleImages(){
      var $active = $('#cycler .active');
      var $next = ($active.next().length > 0) ? $active.next() : $('#cycler img:first');
      $next.css('z-index',2);//move the next image up the pile
      $active.fadeOut(1500,function(){//fade out the top image
      $active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
          $next.css('z-index',3).addClass('active');//make the next image the top one
      });
    }

$(document).ready(function(){
// run every 7s
setInterval('cycleImages()', 2000);
})

</script>


</head>

<body>

<div id="cycler">
<img class="active" src="image1.jpg" alt="My image" />
<img src="image2.jpg" alt="My image" /> 
<img src="image3.jpg" alt="My image" /> 
<img src="image4.jpg" alt="My image" />     
</div>

</body>
</html>


</html>

Comments

0

You're echoing the wrong thing in the inner foreach loop. You should be echoing the key and value, but you're indexing the key.

foreach ($featuredArr as $key => $value) {
   if ($productArr[$key][$value]) {
      echo "key $key=>$value exists:  <br />\n";
      foreach ($productArr[$key][$value] as $newKey => $newValue) {
         echo "$newKey => $newValue<br />\n";
      }
   }
}

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.