Gijs van Tulder

PhD student in machine learning   |   computer scientist   |   the Netherlands

Array to XML function for PHP

This is a simple function that converts a PHP array to XML. You supply the array, the script returns the XML. This function can be useful to send your PHP output to, for example, the Sablotron XSL engine.

Note: If you like what this function does, you might want to take a look at PEAR's XML_Serializer module. It does what this function can do, and probably much more. Sitepoint has a nice introduction.

Run as: array_to_xml($array);

Example

The array:

array('file'=>array(0=>'myfile',
                     1=>'yourfile'),
      'user'=>array(0=>array('name'=>'Foo'),
                    1=>array('name'=>'Bar')),
      'time'=>array('day'=>'tuesday',
                    'week'=>23));
       

is converted to XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<array>
  <file>
    myfile
  </file>
  <file>
    yourfile
  </file>
  <user>
    <name>
      Foo
    </name>
  </user>
  <user>
    <name>
      Bar
    </name>
  </user>
  <time>
    <day>
        tuesday
    </day>
    <week>
        23
    </week>
  </time>
</array>        
       

The script

function array_to_xml($array, $level=1) {
        $xml = '';
    if ($level==1) {
        $xml .= '<?xml version="1.0" encoding="ISO-8859-1"?>'.
                "\n<array>\n";
    }
    foreach ($array as $key=>$value) {
        $key = strtolower($key);
        if (is_array($value)) {
            $multi_tags = false;
            foreach($value as $key2=>$value2) {
                if (is_array($value2)) {
                    $xml .= str_repeat("\t",$level)."<$key>\n";
                    $xml .= array_to_xml($value2, $level+1);
                    $xml .= str_repeat("\t",$level)."</$key>\n";
                    $multi_tags = true;
                } else {
                    if (trim($value2)!='') {
                        if (htmlspecialchars($value2)!=$value2) {
                            $xml .= str_repeat("\t",$level).
                                    "<$key><![CDATA[$value2]]>".
                                    "</$key>\n";
                        } else {
                            $xml .= str_repeat("\t",$level).
                                    "<$key>$value2</$key>\n";
                        }
                    }
                    $multi_tags = true;
                }
            }
            if (!$multi_tags and count($value)>0) {
                $xml .= str_repeat("\t",$level)."<$key>\n";
                $xml .= array_to_xml($value, $level+1);
                $xml .= str_repeat("\t",$level)."</$key>\n";
            }
        } else {
            if (trim($value)!='') {
                if (htmlspecialchars($value)!=$value) {
                    $xml .= str_repeat("\t",$level)."<$key>".
                            "<![CDATA[$value]]></$key>\n";
                } else {
                    $xml .= str_repeat("\t",$level).
                            "<$key>$value</$key>\n";
                }
            }
        }
    }
    if ($level==1) {
        $xml .= "</array>\n";
    }
    return $xml;
}

Update

After I published this script, Jogchem Niemandsverdriet of NobodyHere.com sent me an extended version of the array_to_xml() function that can be used to convert objects. I haven't tested it yet, but if you want to use it on objects: give it a try.

function array_to_xml($array, $level=1) {
        $xml = '';
   // if ($level==1) {
   //     $xml .= "<array>\n";
   // }
    foreach ($array as $key=>$value) {
        $key = strtolower($key);
        if (is_object($value)) {$value=get_object_vars($value);}// convert object to array
        
        if (is_array($value)) {
            $multi_tags = false;
            foreach($value as $key2=>$value2) {
             if (is_object($value2)) {$value2=get_object_vars($value2);} // convert object to array
                if (is_array($value2)) {
                    $xml .= str_repeat("\t",$level)."<$key>\n";
                    $xml .= array_to_xml($value2, $level+1);
                    $xml .= str_repeat("\t",$level)."</$key>\n";
                    $multi_tags = true;
                } else {
                    if (trim($value2)!='') {
                        if (htmlspecialchars($value2)!=$value2) {
                            $xml .= str_repeat("\t",$level).
                                    "<$key2><![CDATA[$value2]]>". // changed $key to $key2... didn't work otherwise.
                                    "</$key2>\n";
                        } else {
                            $xml .= str_repeat("\t",$level).
                                    "<$key2>$value2</$key2>\n"; // changed $key to $key2
                        }
                    }
                    $multi_tags = true;
                }
            }
            if (!$multi_tags and count($value)>0) {
                $xml .= str_repeat("\t",$level)."<$key>\n";
                $xml .= array_to_xml($value, $level+1);
                $xml .= str_repeat("\t",$level)."</$key>\n";
            }
      
         } else {
            if (trim($value)!='') {
             echo "value=$value<br>";
                if (htmlspecialchars($value)!=$value) {
                    $xml .= str_repeat("\t",$level)."<$key>".
                            "<![CDATA[$value]]></$key>\n";
                } else {
                    $xml .= str_repeat("\t",$level).
                            "<$key>$value</$key>\n";
                }
            }
        }
    }
   //if ($level==1) {
    //    $xml .= "</array>\n";
   // }
    return $xml;
}

This is an archived copy of a note that I published on my own site in April 2003.