Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Created December 8, 2018 08:53
Show Gist options
  • Save eto4detak/7544f6d2ee0d7253f7a23539d5b097dc to your computer and use it in GitHub Desktop.
Save eto4detak/7544f6d2ee0d7253f7a23539d5b097dc to your computer and use it in GitHub Desktop.
php parse xml
<?php
public function RecurseXML2($xml = '')
{
function RecurseXML($xml, &$vals, $parent = "")
{
$childs = 0;
$child_count = -1; # Not realy needed.
$arr = array();
foreach ($xml->children() as $key => $value) {
if (in_array($key, $arr)) {
$child_count++;
} else {
$child_count = 0;
}
$arr[] = $key;
$k = ($parent == "") ? "$key.$child_count" : "$parent.$key.$child_count";
foreach($value->attributes() as $a => $b) {
$vals[$k.'.'.$a] = (string) $b;
}
$childs = RecurseXML($value, $vals, $k);
if ($childs == 0) {
$vals[$k] = (string) $value;
}
}
return $childs;
}
$vals = array();
RecurseXML($xml, $vals);
return $vals;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment