Php

How To Convert XML To Associative Array in PHP

Today, I need to convert the XML file into an associative array to store in variables. So I have done a lot of googling and found several methods/views and libraries to convert XML objects into Array, but in stack overflow – I have found below a simple script to convert XML into an array.

I am using file_get_contents() php method to read xml file data into string.

Also, checkout other related tutorials,

In this tutorial, I will let you know how to convert XML into an array in PHP.

Step 1: Sample XML file

Related Post

I have created the below xml sample.xml file, which will use to convert into an array using PHP.

<?xml version='1.0'?>  
<moleculedb>  
    <molecule name='Benzine'>  
        <symbol>ben</symbol>  
        <code>A</code>  
    </molecule>  
    <molecule name='Water'>  
        <symbol>h2o</symbol>  
        <code>K</code>  
    </molecule>
 <molecule name='Parvez'>  
        <symbol>h2o</symbol>  
        <code>K</code>  
    </molecule>
</moleculedb>

Step 2: Convert sample.xml File Into String
Now, I will use file_get_contents() PHP method to read entire file into a string and store into $xmlfile variable.

$xmlfile = file_get_contents($path);
Result:

Step 3: Convert a string of XML into an Object,
We have an XML file as a string, So Let’s convert this XML string into Objects. I will use simplexml_load_string() php method to convert a string of XML into an object.

$ob= simplexml_load_string($xmlfile);
Result:

Step 4: Encode XML Object Into JSON
I will encode the XML object into JSON, the below code will convert XML object into JSON string.

$json  = json_encode($ob);
Result:

Step 5: Decode Json Object
Finally, We will decode JSON to get an array from JSON string.

$configData = json_decode($json, true);
Result:

Full Source Code

$path = "sample.xml"
$xmlfile = file_get_contents($path);
$ob= simplexml_load_string($xmlfile);
$json  = json_encode($ob);
$configData = json_decode($json, true);
echo "<pre>":print_r($configData);

View Comments

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories