After viewing the xml2array function of "http://www.php.net/manual/en/ref.simplexml.php#103617" and the "http://php.net/manual/en/ref.simplexml.php # 112038 "and still have made a change to convert empty tags to null ... I felt the need to work with the result as an object, keeping arrays like arrays. Arriving at this function:
<?php
private function xml2Object($xmlObject, $out = null) {
$out = (is_object($xmlObject) ? new \stdClass : array());
foreach ((array) $xmlObject as $index => $node )
if (is_array($out)) {
$out[$index] = (is_object($node) || is_array($node)) ? (($node instanceof \SimpleXMLElement) && count((array) $node)==0 ? null : $this->xml2Object($node)) : $node;
} else
$out->{$index} = (is_object($node) || is_array($node)) ? (($node instanceof \SimpleXMLElement) && count((array) $node)==0 ? null : $this->xml2Object($node)) : $node;
return $out;
}
?>
And the results of function can be seen below:
<?php
$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8" standalone="no"?><results><l1_elm1><l2_elm1>Some Text</l2_elm1><l2_elm2/></l1_elm1><l1_elm2><l2_elm1><l3_elm1>Array Index[0] - First property</l3_elm1><l3_elm2>Array Index[0] - Second property</l3_elm2><l3_elm3>Array Index[0] - Third property</l3_elm3></l2_elm1><l2_elm1><l3_elm1>Array Index[1] - First property</l3_elm1><l3_elm2>Array Index[1] - Second property</l3_elm2><l3_elm3>Array Index[1] - Third property</l3_elm3></l2_elm1></l1_elm2><l1_elm3/></results>');
var_dump( $this->xml2Object($xml) );
// var_dump xml2Object results
object(stdClass)#339 (3) {
["l1_elm1"]=>object(stdClass)#344 (2) {
["l2_elm1"]=>string(9) "Some Text"
["l2_elm2"]=>NULL
}
["l1_elm2"]=>object(stdClass)#347 (1) {
["l2_elm1"]=>array(2) {
[0]=>object(stdClass)#348 (3) {
["l3_elm1"]=>string(31) "Array Index[0] - First property"
["l3_elm2"]=>string(32) "Array Index[0] - Second property"
["l3_elm3"]=>string(31) "Array Index[0] - Third property"
}
[1]=>object(stdClass)#349 (3) {
["l3_elm1"]=>string(31) "Array Index[1] - First property"
["l3_elm2"]=>string(32) "Array Index[1] - Second property"
["l3_elm3"]=>string(31) "Array Index[1] - Third property"
}
}
}
["l1_elm3"]=>NULL
}
// VarDump Symphony xml2Object results something like
object (#345) {
+"l1_elm1": object (#339) {
+"l2_elm1": "Some Text"
+"l2_elm2": null
}
+"l1_elm2": object (#347) {
+"l2_elm1": array(2) {
[0] => object (#348) {
+"l3_elm1": "Array Index[0] - First property"
+"l3_elm2": "Array Index[0] - Second property"
+"l3_elm3": "Array Index[0] - Third property"
}
[1] => object (#349) {
+"l3_elm1": "Array Index[1] - First property"
+"l3_elm2": "Array Index[1] - Second property"
+"l3_elm3": "Array Index[1] - Third property"
}
]
}
+"l1_elm3": null
}
?>