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

时间:2016-10-25 17:22:24

标签: php arrays multidimensional-array

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

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

我试图从

获取数据的数组
Array
(
    [catalog] => Array
        (
            [comp] => Array
                (
                    [0] => Array
                        (
                            [look] => Array
                                (
                                    [shp] => Array
                                        (
                                            [wok] => Array
                                                (
                                                    [group] => Array
                                                        (
                                                            [customer] => Array
                                                                (
                                                                    [author] => jack
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

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

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [look] => Array
                                (
                                    [shp] => Array
                                        (
                                            [wok] => Array
                                                (
                                                    [group] => Array
                                                        (
                                                            [customer] => Array
                                                                (
                                                                    [author] => jon
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

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

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我正试图获取这样的数据!

我有2个阵列“customer”和“customer1”

我想获得这样的数据

customer =>作者

输出

jack
jon

因为它们在客户数组中

它可以做到吗??

2 个答案:

答案 0 :(得分:1)

假设您的数组存储在$arr中,您将访问comp索引然后循环它,因为它们是数字索引。然后你有一个数组可以减少一些。这一切似乎有点膨胀的数组结构,但将工作

$arr; //Set this to your converted xml
$comps = $arr['catalog']['comp'];

foreach($comps as $comp){
    echo $comp['look']['shp']['wok']['group']['customer']['author'];
}

答案 1 :(得分:0)

<?php
$aVar = Array
(
    'catalog' => Array
        (
            'comp' => Array
                (
                    0 => Array
                        (
                            'look' => Array
                                (
                                    'shp' => Array
                                        (
                                            'wok' => Array
                                                (
                                                    'group' => Array
                                                        (
                                                            'customer' => Array
                                                                (
                                                                    'author' => 'jack',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                ),
                                                            'customer2' => Array
                                                                (
                                                                    'author' => 'lemass',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                )
                                                        )
                                                )
                                        )
                                )
                        ),
                    1 => Array
                        (
                            'look' => Array
                                (
                                    'shp' => Array
                                        (
                                            'wok' => Array
                                                (
                                                    'group' => Array
                                                        (
                                                            'customer' => Array
                                                                (
                                                                    'author' => 'jon',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                ),
                                                            'customer2' => Array
                                                                (
                                                                    'author' => 'kirito',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
);


function findKey($array, $keySearch) {
    foreach ($array as $key => $item) {
        if ($key === $keySearch) {
            echo $item . '<br>';
            //return true; // if just the first is wanted
        } else if (is_array($item) && findKey($item, $keySearch)) {
            return true;
        }
    }
    return false;
}

findKey($aVar, 'author');

打印出来:

  

插孔

     

勒马斯

     

乔恩

     

来源Check if specific array key exists in multidimensional array - PHP