如何从多维数组中提取特定数据 [英] How to pull specific data from multidimensional array

查看:130
本文介绍了如何从多维数组中提取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请帮助!。

已编辑............

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

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

如上所示,我只想获取具有父键tata而不是tata2的值

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

so我可以将它们分别插入数据库中

so I can insert them separately into the database

推荐答案

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

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

这篇关于如何从多维数组中提取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆