Skip to content

Instantly share code, notes, and snippets.

@phaberest
Last active September 20, 2016 13:57
Show Gist options
  • Select an option

  • Save phaberest/763a14a9b15fbb6711addd40a15d668b to your computer and use it in GitHub Desktop.

Select an option

Save phaberest/763a14a9b15fbb6711addd40a15d668b to your computer and use it in GitHub Desktop.

Revisions

  1. phaberest renamed this gist Sep 20, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. phaberest created this gist Sep 20, 2016.
    21 changes: 21 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    private function arrayToXML($data, &$xml)
    {
    foreach ($data as $key => $value) {
    if (is_array($value)) {
    if (is_numeric($key)) {
    $key = 'item'.$key; //dealing with <0/>..<n/> issues
    }
    $subnode = $xml->addChild($key);
    array_to_xml($value, $subnode);
    } else {
    $xml->addChild("$key", htmlspecialchars("$value"));
    }
    }
    }

    private function prepareXML(array $array)
    {
    $xml = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
    $this->array_to_xml($data, $xml);
    return $xml; # Get XML with $->asXML(), save it with $->asXML({path})
    }