?
Solved

Muti dimensional array search in xml parser

Posted on 2006-07-19
3
Medium Priority
?
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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