Enable PHP ADODB handling« an older post
a newer one »Revoke Mysql privileges

simplexml to array

Snippet

Convert a SimpleXML object into a nested array

/**                                                                                                                                                                                  
 * @see http://theserverpages.com/php/manual/en/ref.simplexml.php                                                                                                                    
 */
function simplexml_to_array($xml) {
  if (get_class($xml) == 'SimpleXMLElement') {
    $attributes = $xml->attributes();
    foreach($attributes as $k=>$v) {
      if ($v) $a[$k] = (string) $v;
    }
    $x = $xml;
    $xml = get_object_vars($xml);
  }
  if (is_array($xml)) {
    if (count($xml) == 0) return (string) $x; // for CDATA                                                                                                                           
    foreach($xml as $key=>$value) {
      $r[$key] = simplexml_to_array($value);
    }
    if (isset($a)) $r['@'] = $a;    // Attributes                                                                                                                                    
    return $r;
  }
  return (string) $xml;
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.