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

时间:2016-10-24 14:09:38

标签: php arrays xml multidimensional-array

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

有没有更好的方法来获取特定的列数据而不创建许多循环?因为我想把它们分配给变量。

我试图从

获取数据的数组
 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.
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我删除了所有其他数据以便于阅读,但数组中还有许多其他值。无论如何,我怎样才能获得作者的价值。

echo $array['author']; 

假设我有很多作者数据,而不是上面的例子

请帮忙!

编辑.....................

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

的值

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

2 个答案:

答案 0 :(得分:1)

尝试下面的代码,它将为所有作者提供来自多维数组的数组,而不使用forloops ..,如果你想从multidimentional数组中检索其他值,那么你需要在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);

希望这会有所帮助......

答案 1 :(得分:1)

还可以构建自己的自定义递归函数并从数组中过滤掉所需的值,然后构建自定义数组,例如lolo =&gt; author和lolo1 =&gt; author ....来自多维数组,如下所示... < / p>

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

希望这会有所帮助......