Convert XML to Array in PHP Using XML2Array

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,

  1. attributes stored as key value pairs under [‘tag_name’][‘@attributes’]
  2. CDATA nodes are stored under [‘tag_name’][‘@cdata’]
  3. 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.

Step 3: Call xml to array method and pass xml file as a variable on it.

Demo & Download Source Code Of Convert XMl to Array in PHP