简体   繁体   English

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

[英]How to pull a specific key 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
        (
            [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.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

I'm trying to get the data like this! 我正在尝试获取这样的数据!

I have 2 arrays "customer" and "customer1" 我有2个数组“ customer”和“ customer1”

I want to get the data like this 我想要这样的数据

customer=>author 客户=>作者

the output 输出

jack
jon

because they are in the customer array 因为它们在客户群中

its it possible to do that ?? 它有可能做到这一点?

Say your array is stored in $arr you would access the comp index and then loop it since those are numeric indexes. 假设您的数组存储在$arr您将访问comp索引,然后对其进行循环,因为它们是数字索引。 Then you have an array to whittle down some more. 然后,您有一个阵列可以减少更多内容。 This all seems a bit bloated by the array structure but will work 数组结构似乎使这一切膨胀了,但可以正常工作

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

foreach($comps as $comp){
    echo $comp['look']['shp']['wok']['group']['customer']['author'];
}
<?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');

Prints out: 打印输出:

jack 插口

lemass lemass

jon 乔恩

kirito 基里托

Source Check if specific array key exists in multidimensional array - PHP 检查多维数组中是否存在特定的数组键-PHP

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

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