简体   繁体   中英

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.

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

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'];
}
<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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