I am a beginner, so forgive me all to whom this is trivial...
I have an array, generated from simpleXML object, trying to parse... The array is not symmetrical & I am having trouble finding the right code to make it work. Help & comments are welcomed..
Here is the raw array with print_r:
Array
(
[Area] => Array
(
[0] => Array
(
[Name] => Basement
[id] => 1
[Room] => Array
(
[0] => Array
(
[Name] => Equipment
[id] => 1
)
[1] => Array
(
[Name] => Theater
[id] => 9
)
[2] => Array
(
[Name] => Wine Cellar
[id] => 10
)
)
)
[1] => Array
(
[Name] => 1st Floor
[id] => 2
[Room] => Array
(
[0] => Array
(
[Name] => Kitchen
[id] => 2
)
[1] => Array
(
[Name] => Main Hall
[id] => 4
)
[2] => Array
(
[Name] => Foyer
[id] => 8
)
[3] => Array
(
[Name] => Stone Room
[id] => 14
)
[4] => Array
(
[Name] => Garage Foyer
[id] => 16
)
[5] => Array
(
[Name] => Dining Room
[id] => 17
)
[6] => Array
(
[Name] => Living Room
[id] => 18
)
[7] => Array
(
[Name] => Hearth Room
[id] => 19
)
[8] => Array
(
[Name] => Office
[id] => 20
)
[9] => Array
(
[Name] => Powder Room
[id] => 21
)
)
)
[2] => Array
(
[Name] => 2nd Floor
[id] => 3
[Room] => Array
(
[0] => Array
(
[Name] => Landing
[id] => 7
)
[1] => Array
(
[Name] => Master Bedroom
[id] => 11
)
[2] => Array
(
[Name] => Nannys Room
[id] => 13
)
[3] => Array
(
[Name] => East Kids Bed
[id] => 27
)
[4] => Array
(
[Name] => West Kids Bed
[id] => 28
)
[5] => Array
(
[Name] => Kids Bath
[id] => 29
)
[6] => Array
(
[Name] => Robs Closet
[id] => 31
)
[7] => Array
(
[Name] => Kelli's Office
[id] => 32
)
)
)
[3] => Array
(
[Name] => 3rd Floor
[id] => 7
[Room] => Array
(
[Name] => Bonus room
[id] => 5
)
)
[4] => Array
(
[Name] => Outside Areas
[id] => 8
[Room] => Array
(
[0] => Array
(
[Name] => Veranda
[id] => 22
)
[1] => Array
(
[Name] => Back Outdoor
[id] => 23
)
[2] => Array
(
[Name] => Christmas Lights
[id] => 33
)
[3] => Array
(
[Name] => Front Outdoor
[id] => 34
)
[4] => Array
(
[Name] => Garage
[id] => 35
)
[5] => Array
(
[Name] => Poolhouse Kitchen
[id] => 24
)
[6] => Array
(
[Name] => Poolhouse Utility
[id] => 25
)
)
)
)
)
Here is the code:
foreach($xml2['Area'] as $v){
echo $v['Name'],' <br>';
foreach($v['Room'] as $v2){
echo ' ',$v2['Name'],' <br>';
}
}
Here is the result:
Basement
Equipment
Theater
Wine Cellar
1st Floor
Kitchen
Main Hall
Foyer
Stone Room
Garage Foyer
Dining Room
Living Room
Hearth Room
Office
Powder Room
2nd Floor
Landing
Master Bedroom
Nannys Room
East Kids Bed
West Kids Bed
Kids Bath
Robs Closet
Kelli's Office
3rd Floor
B
5
Outside Areas
Veranda
Back Outdoor
Christmas Lights
Front Outdoor
Garage
Poolhouse Kitchen
Poolhouse Utility
The problem lies with the "3rd Floor" which should contain "Bonus Room" as the room. I know that this is the only element that has no "Rooms" array, but I can't figure out how to code the trap for it. With this data, the condition could exist anywhere, not just my example, but I am stumped with the non symmetrical array that I am unfamiliar with. Any help? Thanks. --akaweed