Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:17:27+00:00 2026-06-17T18:17:27+00:00

how to get an array of xml with php in this format, of an

  • 0

how to get an array of xml with php in this format, of an external file

$locations = array(
  (Bondi Beach, -33.890542, 151.274856, 4),
  (Coogee Beach, -33.923036, 151.259052, 5),
  (Cronulla Beach', -34.028249, 151.157507, 3)
);
//this is my file php
         $dom = new DOMDocument();
            $dom->load('school.xml');
            $students = $dom->getElementsByTagName('student');
            $i = 0; 
            foreach ($students as $student) {
                $locations = $students->item(0)->nodeValue;
                echo $locations;
                $i++;
            }

when I run this code returns the following result:
Bondi Beach -33.890542 151.274856 4 Coogee Beach -33.923036 151.259052 5 Cronulla Beach -34.028249 151.157507 3
What do I have to change so that It return an array with the following format?:

this is my array school.xml

<school>
<student>
<name>Bondi Beanch</name>
<latitude>-33.890542</latitude>
<longitude>151.274856</longitude>
<id>4</id>
</student>
<student>
<name>Coogee Beach</name>
<latitude>-33.923036</latitude>
<longitude>151.259052</longitude>
<id>5</id>
</student>
<student>
<name>Cronulla Beach</name>
<latitude>-34.028249</latitude>
<longitude>151.157507</longitude>
<id>3</id>
</student>
</school>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:17:28+00:00Added an answer on June 17, 2026 at 6:17 pm
    <?php
    $dom = new DOMDocument();
    $dom->load('school.xml');
    $students = $dom->getElementsByTagName('student');
    foreach ($students as $student){
        foreach($student->childNodes as $node){
            switch($node->nodeName){
                case 'name':
                    $name = $node->nodeValue;
                    break;
                case 'longitude':
                    $long = $node->nodeValue;
                    break;
                case 'latitude':
                    $lat = $node->nodeValue;
                    break;
                case 'id':
                    $id = $node->nodeValue;
                    break;
            }
        }       
        $locations[] = array($name,$lat,$long,$id);
    }
    echo '<pre>';
    print_r($locations);
    echo '</pre>';
    ?>
    

    and if you wanted the arrays to be associative you could replace this line:

    $locations[] = array('name'=>$name,'latitude'=>$lat,'longitude'=>$long,'id'=>$id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for some reason i get error 500 in this file: http://apps.sce.ac.il/testxml/parser.php this is the
I would like to get methods from REST XML file via PHP. I have
I'm trying to get call an XML array using PHP, but for whatever reason,
I tried this to format the XML output in a PHP function with the
I'm tring to process xml (website) into php array. I have tried the following
Hello i have somme problems to write a simple XML-RPC client in PHP. This
When I execute this code I get an error saying PHP Fatal error: Call
page.xml.php will not show any output, in firebug I can see the get response
The following test function reads an XML file with PHP's xmlreader and returns a
This script creates a new XML file with the original data and doesn't have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.