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

时间:2015-01-02 12:07:12

标签: php multidimensional-array 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 />";
      foreach ($productArr[$key][$value] as $newKey => $newValue) {
         echo "$newKey['image']<br />";
      }
   }
}

我也想让它回应两个&#34; KC&#34;和&#34; PT&#34;来自$ featuredArr。目前只有#34; Cat&#34;正在输出。

4 个答案:

答案 0 :(得分:1)

$ productArr [$ key] [$ value]已经是一维数组,对于KC =&gt;冰它拥有价值:

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

你必须输出

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

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

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

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

答案 1 :(得分:0)

内部的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/

答案 2 :(得分:0)

不完全是您的问题,但我认为我的滑块示例可能对您有所帮助,请保留一些图像名称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>

答案 3 :(得分:0)

你在内部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