note 83781 deleted from function.simplexml-load-string by cmb
| From: | cmb@php.net | Date: | Sun, 15 Dec 2019 10:16:08 +0000 |
| Subject: | note 83781 deleted from function.simplexml-load-string by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213536@lists.php.net to get a copy of this message | ||
Note Submitter: SmartD
----
A small 'n nice function to extract an XML and return it as an array. If there is a bug, please
let me know here. I testet it for my purposes and it works.
<?php
public function extractXML($xml) {
if (!($xml->children())) {
return (string) $xml;
}
foreach ($xml->children() as $child) {
$name=$child->getName();
if (count($xml->$name)==1) {
$element[$name] = $this->extractXML($child);
} else {
$element[][$name] = $this->extractXML($child);
}
}
return $element;
}
// you can call it this way
$xml = false;
$xml = @simplexml_load_string($xmlstring);
// 1)
if ($xml) {
$array = extractXML($xml);
} else {
$array = false;
}
// 2)
if ($xml) {
$array[$xml->getName()] = extractXML($xml);
} else {
$array = false;
}
?>