<?php // declare data $data = [ 'Taro'=>['age'=>30, 'hobby'=>['Guitar','Piano']], 'Takeshi'=>['age'=>18, 'hobby'=>['Reading']], 'Arisa'=>['age'=>16, 'hobby'=>['Walking','Tea']], 'Sara'=>['age'=>22, 'hobby'=>['Sleeping']] ]; // saving file pass $file = "serialize-test.xml"; // transfer function from php array to XML function array2xml($arr, $xml_obj = NULL) { if ($xml_obj == NULL){ $def = '<?xml version="1.0"?><root></root>'; $xml_obj = new SimpleXMLElement($def); } foreach($arr as $key => $value){ if (is_numeric($key)) $key = "item"; if (is_array($value)){ $subnode = $xml_obj->addChile($key); array2xml($value, $subnode); } else { $v = htmlentities($value); $xml_obj->addChild($key, $v); } } return $xml_obj; } // transfer array to object $xml_obj = array2xml($data); $str = $xml_obj->asXML(); // save to file file_put_contents($file, $str); // read from file $xml2 = simplexml_load_file($file); foreach ($xml2->children() as $it){ $name = $it->getName(); $age = $it->age; echo "$name:$age:"; $hobby = $it->hobby; foreach ($hobby->children() as $h){ echo "($h)"; } echo "\n"; }