Save array as xml

前端 未结 3 1879
再見小時候
再見小時候 2021-01-13 06:55
array(
name => text,
surname => text,
country => text,
date => text
)

1) How can I save this array to file as xml file?

3条回答
  •  北海茫月
    2021-01-13 07:33

    Using SimpleXML

    for #1 (as in How to convert array to SimpleXML)

    ');
      array_walk_recursive($test_array, array ($xml, 'addChild'));
      print $xml->asXML("file.xml");
    

    for #2

    $xml_data_as_object = simplexml_load_file("file.xml")
    

    returns an object representation of the xml data.

    convert the object to an array with:

    $xml_data_as_array = array();
    foreach ($xml_data->root as $children) {
       $xml_data_as_array[] = array(
         "name" => $children->name,
         "surname" => $children->surname,
         "country" => $children->country,
         "date" => $children->date
      );
    }
    

提交回复
热议问题