note 19322 added to function.get-defined-vars
| From: | jgettys at gnuvox dot com | Date: | Sat, 23 Feb 2002 00:09:32 +0000 |
| Subject: | note 19322 added to function.get-defined-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26962@lists.php.net to get a copy of this message | ||
Simple routine to convert a get_defined_vars object to XML.
function obj2xml($v, $indent='') {
while (list($key, $val) = each($v)) {
if ($key == '__attr') continue;
// Check for __attr
if (is_object($val->__attr)) {
while (list($key2, $val2) = each($val->__attr)) {
$attr .= " $key2=\"$val2\"";
}
}
else $attr = '';
if (is_array($val) || is_object($val)) {
print("$indent<$key$attr>\n");
obj2xml($val, $indent.' ');
print("$indent</$key>\n");
}
else print("$indent<$key$attr>$val</$key>\n");
}
}
//Example object
$x->name->first = "John";
$x->name->last = "Smith";
$x->arr['Fruit'] = 'Bannana';
$x->arr['Veg'] = 'Carrot';
$y->customer = $x;
$y->customer->__attr->id='176C4';
$z = get_defined_vars();
obj2xml($z['y']);
will output:
<customer id="176C4">
<name>
<first>John</first>
<last>Smith</last>
</name>
<arr>
<Fruit>Bannana</Fruit>
<Veg>Carrot</Veg>
</arr>
</customer>
--
http://www.php.net/manual/en/function.get-defined-vars.php
http://master.php.net/manage/user-notes.php?action=edit+19322
http://master.php.net/manage/user-notes.php?action=delete+19322
http://master.php.net/manage/user-notes.php?action=reject+19322