Jump to content

multidimensional array & foreach


akaweed

Recommended Posts

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

 

Link to comment
Share on other sites

PFM, I have tried is_array() in about 8 different iterations, on almost all it was always returning true.  I am more interested on which element you think I should be checking, this is what seems to be confusing to me.

 

This forum was my latest action, the before actions have taken me two days & reading 100's of examples from tutorials/websites, but I have not seen one example that shows a nested foreach on a non symmetrical array.  Do you know of any?

 

Thanks for the reply.

Link to comment
Share on other sites

foreach($v['Room'] as $k2=>$v2){
    if(is_array($v2)){
        // your existing code ... $v2['Name'] will exist
    } else {
        // $v2 is the value of each element of $v['Room'][], i.e. $v['Room']['Name'] and $v['Room']['Id']
        // I would probably add a $k2 to the foreach() so that you can do something like -
        if($k2 = 'Name'){
            // code for the name element here...
        }
    }
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.