0

I'm converting XML file into associative array to pull out the data, the problem is that I have to make 10 loops depends on arrays number in order to get the data.

is there a better way to get a specific column data without creating many loops? because I want to assign them to variables.

the array I'm trying to get data from

 Array
(
    [catalog] => Array
        (
            [book] => Array
                (
                    [0] => Array
                        (
                            [took] => Array
                                (
                                    [dodo] => Array
                                        (
                                            [ahmadz] => Array
                                                (
                                                    [lolo] => 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.
                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [took] => Array
                                (
                                    [dodo] => Array
                                        (
                                            [ahmadz] => Array
                                                (
                                                    [lolo] => 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.
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

I removed all other data to make it easier to read, but there are many other values in the array. Anyway, how can I get the value of author for example.

echo $array['author']; 

assuming that I have many author data, not one as the example above

Please help!.

Edited.....................

Array
(
    [catalog] => Array
        (
            [book] => Array
                (
                    [0] => Array
                        (
                            [took] => Array
                                (
                                    [dodo] => Array
                                        (
                                            [ahmadz] => Array
                                                (
                                                    [lolo] => Array
                                                        (
                                                            [tata] => Array
                                                                (
                                                                    [author] => jac1
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                            [tata2] => Array
                                                                (
                                                                    [author] => jack2
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [took] => Array
                                (
                                    [dodo] => Array
                                        (
                                            [ahmadz] => Array
                                                (
                                                    [lolo] => Array
                                                        (
                                                            [tata] => Array
                                                                (
                                                                    [author] => jack3
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                            [tata2] => Array
                                                                (
                                                                    [author] => jack4
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

As you see above I just want to get the value that has parent keys tata not tata2

so I can insert them separately into the database

6
  • I don’t quite see the problem. You can get the author using $array['catalog']['book']['took']['dodo']['ahmadz']['lolo']['author'], without any loops. Maybe you simplified the array so much that the problem is not obvious and it would help to show at least a part of the unmodified input array. Commented Oct 24, 2016 at 14:15
  • I think you'd be better off using an XML parser if you want cleaner code. Something like xpath should allow you to easily grab data from anywhere inside the XML structure. us2.php.net/manual/en/simplexmlelement.xpath.php Commented Oct 24, 2016 at 14:16
  • I tried to pull out the data through XML, but the parents names are different from one to another, therefor I can't write one path to get all data. Also I'm not expert in xml so I tried to convert it to array which slower, but easier!. Commented Oct 24, 2016 at 14:27
  • Converting XML to an array is not the best Idea - you loose information. Keep it an SimpleXMLElement or DOMDocument and use Xpath to fetch parts/values of it. Xpath can be a lot more then just a simple path. Commented Oct 24, 2016 at 14:53
  • @user2565853: OK, so you write that the element names can change, and that the number of elements can change, right? What is then the exact criterion for the array element you want? If it’s the tag name, you’d be far better off using XPath instead of using an array. Commented Oct 24, 2016 at 15:36

2 Answers 2

1

Try below code which will give you all authors as an array from multidimensional array without using forloops.., also if you want to retrieve other values from multidimentional array then you need to pass array key in in_array at if condition and prepare data according to your requirement...

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

Hope this helps....

Sign up to request clarification or add additional context in comments.

5 Comments

Please don’t use a global variable for situations like these (or better: don’t use global at all). If you need to modify a variable in the outer scope from within the closure, define it there, pass it via reference to the closure [i.e.: use (&$varname)] and write to it inside the closure.
Thank you it works flawlessly. I appreciate your help ^^
if we have lolo and lolo2 is it possible to get the data for lolo only ??
Yes it is possible for that you need to build your own customized recursive function which will iterate over your multidimensional array and prepare customized array having values which you want to filter...
Added another answer which will give you customized array as per your requirement...
1

Also it is possible to build your own custom recursive function and filter out required values from array then build custom array like lolo=>author and lolo1 =>author.... from multidimentional array like below...

function my_walk_recursive($your_multidimentional_array, $find_value, &$filtered_array) {
    foreach($your_multidimentional_array as $key => $data) {                   
        if($data[$find_value] != '') {
            $filtered_array[$key] = $data['author'];
            return true;
        }
        elseif(is_array($data) && (!empty($data))) {
            $result = my_walk_recursive($data, $find_value, $filtered_array);
            if($result == true) {
                continue;
            }
        }           
    }
    return $filtered_array;
}
$filtered_array =  array();
$final_array = array();
$final_array = my_walk_recursive($test_array, 'author', $filtered_array);
var_dump($final_array);

Hope this helps....

1 Comment

I really don't know how to thank you for the effort you gave to help me. Thank you very much. I just edited the question to make it clearer. I tried to insert tata in the "$find_value" , but I got nothing. I just want to get the values that has tata key. if you have time for sure. you've done more then enough for me

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.