Convert XML to array is very common functionality in PHP while working with web services.We are normally using XML type data for request and response.I am sharing this tutorial to convert xml to array in php using
XML2Array lib class.
I 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 now i am using XML2Array class for convert xml data into php array. XML2Array is a class which is use to convert XML to an array in PHP.It returns an php array which 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 following Conventions,
- attributes stored as key value pairs under [‘tag_name’][‘@attributes’]
- CDATA nodes are stored under [‘tag_name’][‘@cdata’]
- In case a node has attributes, the value will be stored in [‘tag_name’][‘@value’]
Also checkout other related tutorials,
Simple Usage of XML2Array Class to Convert XML into Array
Step 1: Download XML2Array class from Here.
Step 2: Include this class into your project file.
Step 3: Define xml file or xml variable.
$xml ="<?xml version='1.0'?>
Step 3: Call xml to array method and pass xml file as a variable on it.
$array = XML2Array::createArray($xml);