0

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

2 Answers 2

1

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"];
   }
});
Sign up to request clarification or add additional context in comments.

1 Comment

What does a var dump of $value give you in the above?
0

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"];}}

2 Comments

it output is author : jac1author : jack2author : jack3author : jack4 is want values that has tata key as parent I don't want to get all values
foreach($array["catalog"]["book"] as $key => $value){foreach($value["took"]["dodo"]["ahmadz"]["lolo"] as $key2 => $value2){i f($key2 == "tata"){echo "author : ". $value2["author"];}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.