Created
January 5, 2017 17:03
-
-
Save tvlooy/4d0abe8fffbeb5b8e4e029aaea61e72a to your computer and use it in GitHub Desktop.
SimpleXML to array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function xmlToArray(SimpleXMLElement $xmlObject) : array | |
{ | |
$array = []; | |
foreach ((array) $xmlObject as $elementName => $value) { | |
if (is_object($value)) { | |
if (empty($value)) { | |
$array[$elementName] = ''; | |
} else { | |
$array[$elementName] = xmlToArray($value); | |
} | |
} elseif (is_array($value)) { | |
foreach ($value as $arrayValue) { | |
$array[$elementName][] = xmlToArray($arrayValue); | |
} | |
} else { | |
$array[$elementName] = $value; | |
} | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment