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
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);
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);
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);
Step 5: Decode Json Object
Finally, We will decode JSON to get an array from JSON string.
$configData = json_decode($json, true);
$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);
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
$array = (array) simplexml_load_string($xmlfile);
Gets you the exact same result, without all the unnecessary code.
`$array = (array) simplexml_load_string($xmlfile);`
This doesn't work for tags without content, like `
ok, i ll chek
you need to pass third parameter as LIBXML_NOCDATA for the function simplexml_load_string
its too good.... :D