How to convert array to SimpleXML

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

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

30条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 07:09

    Most of the above answers are correct. However, I came up with this answer which solves the array_walk_recursive compatibility issue and also the numerical keys problem. It also passed all the tests I made:

    function arrayToXML(Array $array, SimpleXMLElement &$xml) {
    
        foreach($array as $key => $value) {
    
            // None array
            if (!is_array($value)) {
                (is_numeric($key)) ? $xml->addChild("item$key", $value) : $xml->addChild($key, $value);
                continue;
            }   
    
            // Array
            $xmlChild = (is_numeric($key)) ? $xml->addChild("item$key") : $xml->addChild($key);
            arrayToXML($value, $xmlChild);
        }
    }   
    

    I have also added a test class for this which you may find useful:

    class ArrayToXmlTest extends PHPUnit_Framework_TestCase {
    
        public function setUp(){ }
        public function tearDown(){ }
    
        public function testFuncExists() {
            $this->assertTrue(function_exists('arrayToXML'));
        }
    
        public function testFuncReturnsXml() {
            $array = array(
                'name' => 'ardi',
                'last_name' => 'eshghi',
                'age' => 31,
                'tel' => '0785323435'
            );
    
            $xmlEl =  new SimpleXMLElement('');
            arrayToXml($array, $xmlEl);
    
            $this->assertTrue($xmlEl instanceOf SimpleXMLElement);
        }
    
        public function testAssocArrayToXml() {
    
            $array = array(
                'name' => 'ardi',
                'last_name' => 'eshghi',
                'age' => 31,
                'tel' => '0785323435'
            );
    
            $expectedXmlEl = new SimpleXMLElement(''); 
            $expectedXmlEl->addChild('name', $array['name']);
            $expectedXmlEl->addChild('last_name', $array['last_name']);
            $expectedXmlEl->addChild('age', $array['age']);
            $expectedXmlEl->addChild('tel', $array['tel']);
    
            $actualXmlEl =  new SimpleXMLElement('');
            arrayToXml($array, $actualXmlEl);
    
            $this->assertEquals($expectedXmlEl->asXML(), $actualXmlEl->asXML());
        }
    
        public function testNoneAssocArrayToXml() {
    
            $array = array(
                'ardi',
                'eshghi',
                31,
                '0785323435'
            );
    
            // Expected xml value
            $expectedXmlEl = new SimpleXMLElement(''); 
            foreach($array as $key => $value)
                $expectedXmlEl->addChild("item$key", $value);
    
            // What the function produces       
            $actualXmlEl =  new SimpleXMLElement('');
            arrayToXml($array, $actualXmlEl);
    
            $this->assertEquals($expectedXmlEl->asXML(), $actualXmlEl->asXML());
        }
    
        public function testNestedMixArrayToXml() {
    
            $testArray = array(
                "goal",
                "nice",
                "funny" => array(
                    'name' => 'ardi',
                    'tel'   =>'07415517499',
                    "vary",
                    "fields" => array(
                        'small',
                        'email' => 'ardi.eshghi@gmail.com'
                    ),
    
                    'good old days'
    
                ),
    
                "notes" => "come on lads lets enjoy this",
                "cast" => array(
                    'Tom Cruise',
                    'Thomas Muller' => array('age' => 24)
                )
            );
    
            // Expected xml value
            $expectedXmlEl = new SimpleXMLElement(''); 
            $expectedXmlEl->addChild('item0', $testArray[0]);
            $expectedXmlEl->addChild('item1', $testArray[1]);
            $childEl = $expectedXmlEl->addChild('funny');
            $childEl->addChild("name", $testArray['funny']['name']);
            $childEl->addChild("tel", $testArray['funny']['tel']);
            $childEl->addChild("item0", "vary");
            $childChildEl = $childEl->addChild("fields");
            $childChildEl->addChild('item0', 'small');
            $childChildEl->addChild('email', $testArray['funny']['fields']['email']);
            $childEl->addChild("item1", 'good old days');
            $expectedXmlEl->addChild('notes', $testArray['notes']);
            $childEl2 = $expectedXmlEl->addChild('cast');
            $childEl2->addChild('item0', 'Tom Cruise');
            $childChildEl2 = $childEl2->addChild('Thomas Muller');
            $childChildEl2->addChild('age', $testArray['cast']['Thomas Muller']['age']);
    
            // What the function produces       
            $actualXmlEl = new SimpleXMLElement('');
            arrayToXml($testArray, $actualXmlEl);
    
            $this->assertEquals($expectedXmlEl->asXML(), $actualXmlEl->asXML());
        }
    }      
    

提交回复
热议问题