Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: zero minus zero?
(Example: nine)

The Note You're Voting On

guilhermeasn at yahoo dot com dot br
5 years ago
Class with a method to transform array or object to XML:

class MakeXML {

public static function array_obj_to_xml($data, \SimpleXMLElement &$xmlObj) {
foreach((array) $data as $key => $value) {
if(is_numeric($key)) {
$key = "n" . $key;
}
if(is_array($value) or is_object($value)) {
$subnode = $xmlObj->addChild($key);
self::array_obj_to_xml($value, $subnode);
} else {
$xmlObj->addChild($key, htmlspecialchars($value));
}
}
}

}

Example:

$object = new \stdClass();
$object->example = "Try this class";

$objxml = new \SimpleXMLElement('<API/>');
MakeXML::array_obj_to_xml($objxml, $objxml);
echo $objxml->asXML();

<< Back to user notes page

To Top