How to convert array to SimpleXML

前端 未结 30 2558
遇见更好的自我
遇见更好的自我 2020-11-21 06:52

How can I convert an array to a SimpleXML object in PHP?

30条回答
  •  清酒与你
    2020-11-21 07:23

    I found this solution similar to the original problem

     'blub',
      'foo' => 'bar',
      'another_array' => array (
        'stack' => 'overflow',
      ),
    );
    
    class NoSimpleXMLElement extends SimpleXMLElement {
     public function addChild($name,$value) {
      parent::addChild($value,$name);
     }
    }
    $xml = new NoSimpleXMLElement('');
    array_walk_recursive($test_array, array ($xml, 'addChild'));
    print $xml->asXML();
    

提交回复
热议问题