note 19322 modified in function.get-defined-vars by danbrown

From: Date: Fri, 06 Feb 2009 00:53:49 +0000
Subject: note 19322 modified in function.get-defined-vars by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150285@lists.php.net to get a copy of this message
Simple routine to convert a get_defined_vars object to XML. <?php 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> --was-- 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://php.net/manual/en/function.get-defined-vars.php

« previous php.notes (#150285) next »