Solved

Muti dimensional array search in xml parser

Posted on 2006-07-19
3
169 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
ID: 17164214
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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