in this post. I am going to tell you about converting XML file data into PHP array, I am using XML2Array libs, I am also letting you know how to convert XML to array without any libs as well.
Converting XML to array is a very common functionality in PHP while working with web services. We are normally using XML type data for requests and responses between server and client communication. I am sharing this tutorial to convert xml to array in php using XML2Array
lib class.
I have already shared a post How To Convert XML To Associative Array in PHP, In that post, i am using simplexml_load_string()
php function.
But, I am using XML2Array class to convert XML data into PHP array.
The XML2Array is a class that is used to convert XML to an array in PHP. It returns a PHP array that can be converted back to XML using the Array2XML class. This class use string XML as input or an object of type DOMDocument.
As per docs, There are the following Conventions,
Also checkout other related tutorials,
Let’s create an array from an XML file by following the steps.
Step 1: Download XML2Array class from Here.
Step 2: We are using XML2Array libs to convert xml file data into the PHP array, Let’s download XML2Array library.
include_once("xml2php.php");
Step 3: Now we will create a php variable that will hold xml file content. The following code is defined as a xml_data
name variable and assigned xml file content.
$xml_data= "<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> Don't forget me this weekend! </note>"
Step 4: Create PHP array data from XML file using the inbuilt createArray() method, This method takes a variable that holds XML file content. We’ll pass XML file as a variable on it.
$array = XML2Array::createArray($xml);
include_once("xml2php.php"); $xml_data= "<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> Don't forget me this weekend! </note>" $array = XML2Array::createArray($xml_data); print_r($array);
We can also use the simplexml_load_string()
method to transform XML into a PHP array. Using the file_get_contents()
method, we’ll read a file and convert it to PHP object data using the simplexml_load_string()
method.
Then, using the json_encode()
method, we’ll transform the object to JSON data, which will then be converted to an array using the json_decode()
method.
<?php // xml file path or locaton $path = "test.xml"; // Read entire file into string variable $xmlfile = file_get_contents($path); // Convert xml string into an PHP object $xml_obj = simplexml_load_string($xmlfile); // Convert into json Data $json_data = json_encode($xml_obj); // Convert into associative array $arr_data = json_decode($json_data, true); print_r($arr_data); ?>
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