简体   繁体   English

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

[英]How to pull a specific data from multidimensional array

I have a problem that I have to make 10 loops depends on arrays number in order to get the data. 我有一个问题,我必须使10个循环依赖于数组号才能获取数据。

I fixed that by some help from my other post, but I have one issue renaming 我通过其他帖子的一些帮助解决了该问题,但我有一个重命名问题

The array I'm using 我正在使用的数组

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

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

And this how I get a specific data from it 这就是我从中获取特定数据的方式

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

The problem I have that before these values 在这些值之前我有一个问题

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

I have different keys 我有不同的钥匙

tata and tata2 塔塔和塔塔2

I want to get only the values in "tata" key 我只想获取“ tata”键中的值

but the code above is returning both values from "tata" and "tata2" keys 但是上面的代码从“ tata”和“ tata2”键返回了两个值

Please help me to get the data from one key not both 请帮助我从一个键获取数据,而不是两个

This is a bit of a sloppy method, but it should work. 这有点草率,但是应该可以。 Simply look for the tata key and then extract the author field from that. 只需查找塔塔密钥,然后从中提取作者字段。

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

simply use nested foreach loop 只需使用嵌套的foreach循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM