dosf40815
2016-10-25 17:22
采纳率: 100%
浏览 46
已采纳

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

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
        (
            [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"

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpian6319 2016-10-25 17:34
    已采纳

    Say your array is stored in $arr you would access the comp index and then loop it since those are numeric indexes. 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'];
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanmianxue2687 2016-10-25 18:14
    <?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

    jon

    kirito

    Source Check if specific array key exists in multidimensional array - PHP

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题