note 21839 deleted from ref.array by sniper
| From: | sniper@php.net | Date: | Sat, 18 Oct 2003 18:05:01 +0000 |
| Subject: | note 21839 deleted from ref.array by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58847@lists.php.net to get a copy of this message | ||
Note Submitter: josh@mirtec.net
----
here's a little function i wrote to convert any multidimensional array to an xml tree. it has
this prototype:
string arr2xml (array array, [string tree_name], [[int level])
use the second argument if you want to specify a name for the top of the tree - otherwise it
defaults to level0. the first call to the function is kind of a dummy top level that wraps the array
inside another array and calls the function for real.
dont use the third argument. thats used to keep track of the levels in all the recursive calls.
heres the code:
function arr2xml ($arr)
{
if (func_num_args () < 3)
{
$wrapper = (func_num_args < 2) ? array ($arr) : array (func_get_arg(1)=>$arr);
$xml = arr2xml ($wrapper, '', 0);
}
else
{
$level = func_get_arg (2);
while (list ($key, $val) = each ($arr))
{
if ($key === (int)$key) $key = 'level'.$level;
$xml .= '<'.$key.'>';
if (gettype ($val) == 'array')
{
$xml .= arr2xml ($val, '', $level+1);
}
else
{
$xml .= $val;
}
$xml .= '</'.$key.'>';
}
}
return $xml;
}