Last active
January 5, 2017 18:51
-
-
Save troelskn/221aec3bfdfd3d86d069 to your computer and use it in GitHub Desktop.
xml2assoc.php
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 | |
// Take XmlReader node and read it into an assoc array | |
function xml2assoc($xml) { | |
$buffer = null; | |
while ($xml->read()) { | |
switch ($xml->nodeType) { | |
case XMLReader::END_ELEMENT: | |
return $buffer; | |
case XMLReader::ELEMENT: | |
if (!is_array($buffer)) { | |
$buffer = array(); | |
} | |
$buffer[$xml->name] = $xml->isEmptyElement ? null : xml2assoc($xml); | |
if ($xml->hasAttributes) { | |
while ($xml->moveToNextAttribute()) { | |
$buffer[$xml->name] = $xml->value; | |
} | |
} | |
break; | |
case XMLReader::TEXT: | |
case XMLReader::CDATA: | |
if (!is_string($buffer)) { | |
$buffer = ""; | |
} | |
$buffer .= $xml->value; | |
} | |
} | |
return $buffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, only last item of array is presented in result assoc array.
->>