- simplexml_load_string: documentation ( source)
- print_r: documentation ( source)
- error_reporting: documentation ( source)
- array_keys: documentation ( source)
<?php
error_reporting(E_ALL);
global $n;
$n = 0;
function convert($xml)
{
$simpleXmlObj = simplexml_load_string($xml);
$array = toarray($simpleXmlObj);
return $array;
}
function toarray($simpleXmlObj)
{
$simpleXmlObjNew = array();
foreach (array_keys((array) $simpleXmlObj) as $element) {
$el = (array) $simpleXmlObj->$element;
if (isset($el[0])) {
if (is_object($el[0])) {
$r = (array) $el[0];
if (isset($r[0])) {
$el[0] = $r[0];
}
}
$trim = $el[0];
$simpleXmlObjNew[$element] = $trim;
} else {
$simpleXmlObjNew[$element] = toarray($simpleXmlObj->$element);
}
if (empty($simpleXmlObjNew[$element])) {
$simpleXmlObjNew[$element] = '';
}
}
return $simpleXmlObjNew;
}
$xml = <<<xml
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<e></e>
<e1> </e1>
<e2> s </e2>
<e3>s</e3>
<e4>
</e4>
</RESPONSE>
xml;
$simpleXmlObj = simplexml_load_string($xml);
$response = toarray($simpleXmlObj);
print_r($response);