Ejemplo n.º 1
0
 /**
  * Convert an XML document to a multi dimensional array
  * Pass in an XML document (or SimpleXMLElement object) and this recrusively loops through and builds a representative array
  *
  * @param string $xml - XML document - can optionally be a SimpleXMLElement object
  * @return array ARRAY
  */
 public static function toArray($xml)
 {
     if (is_string($xml)) {
         $xml = new SimpleXMLElement($xml);
     }
     $children = $xml->children();
     if (!$children) {
         return (string) $xml;
     }
     $arr = array();
     foreach ($children as $key => $node) {
         $node = modRestArrayToXML::toArray($node);
         // support for 'anon' non-associative arrays
         if ($key == 'anon') {
             $key = count($arr);
         }
         // if the node is already set, put it into an array
         if (isset($arr[$key])) {
             if (!is_array($arr[$key]) || $arr[$key][0] == null) {
                 $arr[$key] = array($arr[$key]);
             }
             $arr[$key][] = $node;
         } else {
             $arr[$key] = $node;
         }
     }
     return $arr;
 }