How to convert simplexml object to array in php

Steps:

1. First convert your xml data into an object using simplexml_load_string() function . 

   $xml = simplexml_load_string($xmlData);

2. The simplexmlelement class provides a method called children() which provides the children of an element.

        function xml2array($xml)

      {

          $arr = array();

          foreach ($xml->children() as $r)

         {

              $t = array();

              if (count($r->children()) == 0)

             {

                  $arr[$r->getName()] = strval($r);

             }

             else

             {

                 $arr[$r->getName()][] = xml2array($r);

             } 

         }

         return $arr;

    }

In the above function, count($r->children()) == 0 signifies that if the children count of a simplexmlelement object is 0 it can be taken as an empty node and shown as a string in the array.

$arr[$r->getName()] = strval($r)  in this getName method gives the name of the tag; $r is a Simplexmlelement object and to get its value strval should be used.