1 Answer
Alok Prasad

To convert the XML document into PHP array, we have to utilize some PHP functions. The procedure is explained underneath with an example.

Step 1:

We have to create an XML file that needs to convert into the array.

<?xml version='1.0'?>
<firstname name='Alex'>
<firstname name='Sandra'>

Step 2:

The above XML file will import into PHP using file_get_contents() function which read the entire file as a string and stores into a variable.

Step 3:

After the above step, we can easily convert the string into an object through the inbuilt functions simplexml_load_string() of PHP.

Step 4:

After the above step, we can use json_encode() function to present the object in the json encoded string.

Step 5:

The json_decode() function decode a JSON string. It converts a JSON encoded string into a PHP array.


// xml file path
   $path = "abc.xml";
   $xmlfile = file_get_contents($path);
   $new = simplexml_load_string($xmlfile);
   $jsonfile = json_encode($new);
   $myarray = json_decode($jsonfile, true);


We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.