Solved

Muti dimensional array search in xml parser

Posted on 2006-07-19
3
166 Views
Last Modified: 2008-03-06
Hai,
I got xml parser to read xml file the output is like multi dimensional array ,i want to search particular id and get that matched id details from that array if the element have child to read child array also
I give one example here

<map>
<page id="home" name="Home" content="home.xml" template="home.php" bar="home_bar.xml"/>
<page id="training" name="Training" content="training.xml" template="text.php" bar="training_bar.xml">
      <page id="training-trainings" name="Trainings" content="training-trainings.xml" template="text.php" bar="training_bar.xml"/>
      <page id="training-workshops" name="Workshops" content="training-workshops.xml" template="text.php" bar="training_bar.xml"/>
</page>
</map>

If i give search id ="training"
The out put is the values in
 id="training"
 name="Training"
 content="training.xml"
 template="text.php"
 bar="training_bar.xml"

2.If i give search id ="home"
The out put is the values in
 id="home"
 name="Home"
 content="home.xml"
template="home.php"
 bar="home_bar.xml"

and child nodes values are need
 id="training"
 name="Training"
 content="training.xml"
 template="text.php"
 bar="training_bar.xml"

2nd child
 id="training-trainings"
 name="Trainings"
 content="training-trainings.xml"
 template="text.php"
 bar="training_bar.xml"
      <page id="training-workshops" name="Workshops" content="training-workshops.xml" template="text.php" bar="training_bar.xml"/>

My Xml parser out put is like this
Array
(
    [map] => Array
        (
            [page] => Array
                (
                    [0] => Array
                        (
                            [id] => home
                            [name] => Home
                            [content] => home.xml
                            [template] => home.php
                            [bar] => home_bar.xml
                            [value] =>
                        )

                    [1] => Array
                        (
                            [page] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => training-trainings
                                            [name] => Trainings
                                            [content] => training-trainings.xml
                                            [template] => text.php
                                            [bar] => training_bar.xml
                                            [value] =>
                                        )

                                    [1] => Array
                                        (
                                            [id] => training-workshops
                                            [name] => Workshops
                                            [content] => training-workshops.xml
                                            [template] => text.php
                                            [bar] => training_bar.xml
                                            [value] =>
                                        )

                                )

                            [id] => training
                            [name] => Training
                            [content] => training.xml
                            [template] => text.php
                            [bar] => training_bar.xml
                            [value] =>
                        )

                )

            [value] =>
        )

)



Any Expert help me

Thanks
Bala
0
Comment
Question by:gstalin
3 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
Comment Utility
Lets say the array is stored in $xmlArray, then you should be able to use:

function search($xmlArray, $searchID) {
  foreach($xmlArray['map']['page'] as $key => $pageInfo) {
    if($pageInfo['id'] == $searchID) {
        return $pageInfo;
    }
  }
  return false;
}

Then call it like:

$result = search($xmlArray, 'home');

I hope this helps,

-r-
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now