note 21839 added to ref.array
| From: | josh at mirtec dot net | Date: | Wed, 29 May 2002 00:17:26 +0000 |
| Subject: | note 21839 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31133@lists.php.net to get a copy of this message | ||
could an admin please remove my last post? this is a much better rewrite of the arr2xml () function.
its more sensibly written and flexible now. theres only one required argument, the array itself. 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;
}
--
http://www.php.net/manual/en/ref.array.php
http://master.php.net/manage/user-notes.php?action=edit+21839
http://master.php.net/manage/user-notes.php?action=delete+21839
http://master.php.net/manage/user-notes.php?action=reject+21839