如何从多维数组中提取特定数据

时间:2016-10-24 20:57:32

标签: php arrays multidimensional-array

我有一个问题,我必须使10个循环取决于数组编号才能获得数据。

我通过其他帖子的帮助解决了这个问题,但我有一个问题重命名

我正在使用的数组

Array
(
    [catalog] => Array
        (
            [book] => Array
                (
                    [0] => Array
                        (
                            [took] => Array
                                (
                                    [dodo] => Array
                                        (
                                            [ahmadz] => Array
                                                (
                                                    [lolo] => Array
                                                        (
                                                            [tata] => Array
                                                                (
                                                                    [author] => ahmadz
                                                                    [title] => Midnight Rain
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies, 
                              an evil sorceress, and her own childhood to become queen 
                              of the world.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [took] => Array
                                (
                                    [dodo] => Array
                                        (
                                            [ahmadz] => Array
                                                (
                                                    [lolo] => Array
                                                        (
                                                            [tata2] => Array
                                                                (
                                                                    [author] => Ralls, Kim
                                                                    [title] => Midnight Rain
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies, 
                              an evil sorceress, and her own childhood to become queen 
                              of the world.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

这就是我如何从中获取特定数据

$author_array = array();
array_walk_recursive($array, function($value, $key) {
    if (in_array($key, array("author"))) {
       echo  $author_array[] = $value;
    }
});

在这些值之前我遇到的问题

 [author] => ahmadz
 [title] => Midnight Rain
 [genre] => Fantasy
 [price] => 5.95

我有不同的钥匙

tata和tata2

我想只获取“tata”键中的值

但上面的代码从“tata”和“tata2”键返回两个值

请帮我从一个键中获取数据而不是两个

2 个答案:

答案 0 :(得分:1)

这是一个草率的方法,但它应该工作。只需查找tata密钥,然后从中提取作者字段。

$author_array = array();
array_walk_recursive($array, function($value, $key) {
   if (in_array($key, array("tata"))) {
      echo  $author_array[] = $value["author"];
   }
});

答案 1 :(得分:0)

只需使用嵌套的foreach循环

foreach($ array [“catalog”] [“book”] as $ key => $ value){foreach($ value [“took”] [“dodo”] [“ahmadz”] [“lolo” ] as $ key2 => $ value2){echo“author:”。 $ 2 [ “作者”];}}