Solved

Muti dimensional array search in xml parser

Posted on 2006-07-19
3
167 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wamp versus xampp 4 45
<? versus <?php 5 37
Download a website to hdd 2 50
get domain with php 7 20
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 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

20 Experts available now in Live!

Get 1:1 Help Now