【问题标题】:How to pull a specific key data from multidimensional array如何从多维数组中提取特定的关键数据
【发布时间】:2016-10-25 17:22:23
【问题描述】:

我正在将 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”

我想得到这样的数据

客户=>作者

输出

jack
jon

因为它们在客户数组中

有可能吗??

【问题讨论】:

  • 直到客户的钥匙总是一样的? catalog, comp, #, look 等等...
  • 另外,当您从 XML 创建数组时,您可能希望创建更易于管理的数组。

标签: php arrays multidimensional-array


【解决方案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'];
}

【讨论】:

  • 谢谢 真的很简单0---0
【解决方案2】:
<?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

【讨论】:

  • 这是一个很棒的功能,但它会打印 customer 和 customer2 数组。我只想打印客户数组。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 2020-06-11
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
相关资源
最近更新 更多