Thursday, February 21, 2008

PHP array2xml() - Coverts An Array To XML Structure


/*
* Function: array2xml()
*
* Description: Array to XML is a function that does just that... converts any given
* array to xml structure.
*
* Version: 1.0
*
* Created by: Marcus Carver © 2008
* Email: info@marcuscarver.com
* Link: http://blog.marcuscarver.com/
*
* Arguments : $array - The array you wish to convert into a XML structure.
* $name - The name you wish to enclose the array in, the 'parent'
* tag for XML.
* $standalone - This will add a document header to identify output
* solely as a XML document.
* $beginning - INTERNAL USE... DO NOT USE!
*
* Return: Gives a string output in a XML structure
*
* Use: echo array2xml($products,'products');
* die;
*/

function array2xml($array, $name='array', $standalone=TRUE, $beginning=TRUE) {

global $nested;

if ($beginning) {
if ($standalone) header("content-type:text/xml;charset=utf-8");
$output .= '<'.'?'.'xml version="1.0" encoding="UTF-8"'.'?'.'>' . LF;
$output .= '<' . $name . '>' . LF;
$nested = 0;
}

// This is required because XML standards do not allow a tag to start with a number or symbol, you can change this value to whatever you like:
$ArrayNumberPrefix = 'ARRAY_NUMBER_';

foreach ($array as $root=>$child) {
if (is_array($child)) {
$output .= str_repeat(" ", (2 * $nested)) . ' <' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . LF;
$nested++;
$output .= array2xml($child,NULL,NULL,FALSE);
$nested--;
$output .= str_repeat(" ", (2 * $nested)) . ' ' . LF;
}
else {
$output .= str_repeat(" ", (2 * $nested)) . ' <' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . LF;
}
}

if ($beginning) $output .= '';

return $output;
}