简体   繁体   English

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

[英]How to pull specific data from multidimensional array

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. 我正在将XML文件转换为关联数组以提取数据,问题是我必须根据数组编号进行10次循环才能获取数据。

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 如您在上面看到的,我只想获取具有父键tata而不是tata2的值

so I can insert them separately into the database 所以我可以将它们分别插入数据库

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... 尝试下面的代码,它将为您提供所有作者作为多维数组的数组,而无需使用forloops ..,同样,如果您想从多维数组中检索其他值,则需要在if条件下在in_array中传递数组键,并根据您的条件准备数据需求...

$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.... 希望这可以帮助....

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... 也可以构建自己的自定义递归函数并从数组中过滤出所需的值,然后从多维数组中构建自定义数组,例如lolo => author和lolo1 => author ....,如下所示...

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.... 希望这可以帮助....

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

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