Home > PHP > How to Convert XML to Array in PHP?

How to Convert XML to Array in PHP?

In this tutorial, you will learn about how to convert an XML file to Array with PHP. We will parse the XML code into an Array using the PHP functions.

We will use file_get_contents(), simplexml_load_string(), json_encode() and json_decode() functions. These PHP built-in functions will help us to convert the XML file into PHP array.

Let’s get started!

Create an XML File

XML is an eXtensible Markup Language same as HTML but it does not have predefined tags to use. You can create your own tags relative to values and use them.

XML was designed to store and transport data with self-descriptive tags. It is fully independent to create our own tags corresponding to data needs and then we can store data in XML and search later, and share with others.

So first, we will create an XML file. You can take any such information for example to show in an XML file and store in it. See the following sample XML file, you can copy it for reference.

You can download this file here.

Convert XML File to Array with PHP

Now we will use the above XML file to convert it into an array with PHP functions. It is very simple and easy to do that, you just have to read the file and then convert XML strings into an object.

Then we can parse the XML and convert the XML object into JSON and decode it to print the PHP associative array.

OK. let’s get understand line by line.

XML file path variable

So the first thing you need to do is declare a variable that has the XML file path. The path could be from your machine or online.

Read the XML file

Now we will read the XML file using the file_get_contents() function by giving the parameter referring to the above path variable.

Convert XML string into an object

This is the main step to convert the XML file to an array in PHP. So we will use the simplexml_load_string() PHP function to parse the XML file. It will make an object from the XML string.

Convert XML object to JSON

Now we will use the json_encode() PHP function to convert the XML object into JSON object. Then it will return JSON representation value.

Convert into associative array

This is the final step, in this step, we will use the json_decode() function to decode the JSON data into PHP associative array.

Note: Don’t forget to pass the true value of the second parameter of json_decode. If the value is true then data will return as an associative array and if the value is false then data will be object.

OK. Now you can print the $dataArr variable anywhere using the print_r() function.

See All Code Together

In the above steps, I have explained one by one line about how to convert an XML file or data into a PHP array and how it works.

You can check out the following code same as the above code but all together.

Hope it is helpful for you. Please share it with your friends. If you have any questions about this tutorial or anything please don’t hesitate to ask me. Comment your query I will respond to you as soon as possible.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment