我如何回应这个阵列中的流派?

时间:2014-11-01 22:24:39

标签: php

我试图用回声显示电影类型......所以恐怖,动作和惊悚片会像这样出现

恐怖 行动 惊悚片

我知道我之间需要断点,但我无法弄清楚如何打印这些阵列的键。

<?php
        $movies = array (
         "horror"=>array (
         "Paranormal Activity"=> array (
         "Katie Featherston",
         "Kathryn Newton"
         ),
         "The Ring"=> array (
         "Naomi Watts",
         "Brian Cox"
         ),
         "Sleepy Hollow"=> array(
         "Johhny Depp",
         "Christina Ricci"
         )
         ),
         "action" => array (
         "The Tourist"=> array (
         "Johnny Depp",
         "Angelina Jolie"
         ),
         "Public Enemies"=> array (
         "Johnny Depp", "Christian Bale"
         ),
         "The Dark Knight Rises"=> array (
         "Christian Bale",
         "Tom Hardy"
         ),
         "Salt"=> array (
         "Angelina Jolie"
         )
         ),
         "thriller"=> array (
         "Inception"=> array(
         "Leornardio Dicaprio",
         "Tom Hardy"
         ),
         "Changeling" => array(
         "Angelina Jolie"
         ),
         "The Game"=> array(
         "Michael Douglas",
         "Sean Penn"
         )
         )
        );

        foreach ($movies as $x)
        {
            echo $x;
            echo "</br>";
        }

已编辑

foreach ($movies as $key=>$x)
        {
            echo $key;
            echo "</br>";
        }
        ?>

1 个答案:

答案 0 :(得分:0)

虽然前面的答案有效,但如果你想让代码更自我记录,你也可以这样做:

<?php
$keys = array_keys( $movies );
foreach ( $keys as $genre ) {
    echo $genre,"\n";
}