/** * Convertis un xml_object en array * @param xml_object $xml_object * @param array $out * @return array */ public static function xml_2_array($xml_object, $out = array()) { foreach ((array) $xml_object as $index => $node) { $out[$index] = is_object($node) ? xml_2_array($node) : $node; } return $out; }
<?php require __DIR__ . '/../vendor/autoload.php'; $array = array('abc' => array('cba' => 'abc', 'abc' => '')); echo array_2_xml($array), "\n"; $structure = '<?xml version="1.0" encoding="utf-8"?>'; echo array_2_xml($array, $structure), "\n"; $json = '{"abc":{"cba":"abc","abc":""}}'; echo json_2_xml($json), "\n"; $structure = '<?xml version="1.0" encoding="utf-8"?>'; echo json_2_xml($json, $structure), "\n"; $xml = '<?xml version="1.0"?><abc><cba>abc</cba><abc/></abc>'; echo var_export(xml_2_array($xml)), "\n"; echo xml_2_json($xml), "\n";
/** * @dataProvider arrayWithXml */ public function testXmlToArray(array $data, $string) { $this->assertEquals($data, xml_2_array($string)); }