How To Read XML Data To Array in PHP Tutorial

Reading Time: 3 minutes
1,737 Views

Inside this article we will see the concept i.e How To Read XML Data To Array in PHP. Article contains classified information about reading XML data and convert into PHP array. You can use this concept to seed your MySQLi database table.

If you are looking for an article which gives the understanding about parsing a XML data into PHP Array then this article will help you to learn a lot from this tutorial.

The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more.

Learn More –

Let’s get started.

Application Programming

Suppose, you have a XML file named as books.xml. This XML file contains books data. We will parse this XML data to PHP Array.

Create a file called it as index.php inside your project folder where books.xml file kept.

Open index.php and write this code into it.

<?php
/*
 @Author: Sanjay Kumar
 @Project: How To Read XML Data To Array in PHP Tutorial
 @Email: onlinewebtutorhub@gmail.com
 @Website: https://onlinewebtutorblog.com/
*/

$xmlString = file_get_contents('books.xml');
$xmlObject = simplexml_load_string($xmlString);

$json = json_encode($xmlObject);
$phpArray = json_decode($json, true);

echo "<pre>";
print_r($phpArray);

Application Testing

Now,

Open into browser.

URL: http://localhost/php-xml-to-array/index.php

You can easily save or seed this PHP array into your database table.

We hope this article helped you to learn How To Read XML Data To Array in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more