jack jack - 1 month ago 8
PHP Question

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

Answer

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