简体   繁体   中英

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.

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

I want to get only the values in "tata" key

but the code above is returning both values from "tata" and "tata2" keys

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($array["catalog"]["book"] as $key => $value){foreach($value["took"]["dodo"]["ahmadz"]["lolo"] as $key2 => $value2){echo "author : ". $value2["author"];}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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