如何输出多维数组的一部分?

How to output part of a multidimensional array?

我试图从数组中回显图像,但没有成功。

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

我还希望它从 $featuredArr 回显 "KC" 和 "PT"。目前只有"Cat"正在输出。

$productArr[$key][$value] 已经是一维数组,对于 KC => Ice,它包含值:

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

你只需要输出

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

此外,您应该使用 isset() 或 empty() 来检查元素是否存在,例如:

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

这可以防止未定义的索引错误。

内部的 foreach 循环是不必要的,而且会做一些奇怪的事情。尝试 运行 这个,它似乎有效:

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

您可以使用 PHP repl 站点快速测试此类内容,例如:

http://phpepl.herokuapp.com/

不完全针对您的问题,但我认为我的滑块示例可能对您有所帮助,请在同一文件夹中保留一些名称为 image1.jpg image2.jpg... 的图像

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

你在内部 foreach 循环中回显了错误的内容。您应该回显键和值,但您正在索引键。

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