我是php xml的新手。我有下面的PHP代码
$query = mysql_query("SELECT u.name, u.phone, m.email, m.mobile FROM user u, micards m WHERE m.usr_id=u.id ");
while ( $row[] = mysql_fetch_assoc($query))
}
foreach(array_filter($row) as $key =$value) {
$output[$value['phone']]['cards'][] = array(
'email' =$value['email'],
'mobile' =$value['mobile'],
'name' =$value['name']
);}
任何人都可以告诉我如何以下面的格式获取xml我想要的xml?
<phone>
<cards>
<email>..</email>
<mobile>..</mobile>
<name>..</name>
</cards>
<cards>
<email>..</email>
<mobile>..</mobile>
<name>..</name>
</cards>
</phone>
<phone>
<cards>
<email>..</email>
<mobile>..</mobile>
<name>..</name>
</cards>
<cards>
<email>..</email>
<mobile>..</mobile>
<name>..</name>
</cards>
</phone>
答案 0 :(得分:0)
我希望这会对你有所帮助
$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><phone></phone>");
$tour_xml_path=set your xml file path;
array_to_xml1($data_array,$xml_student_info,$tour_xml_path);
function array_to_xml1($student_info, &$xml_student_info,$file_name) {
foreach($student_info as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_student_info->addChild("$key");
array_to_xml1($value, $subnode,$file_name);
}
else{
$subnode = $xml_student_info->addChild("item$key");
array_to_xml1($value, $subnode,$file_name);
}
}
else {
$xml_student_info->addChild("$key",htmlspecialchars("$value"));
}
}
$xml_student_info->asXML($file_name);
}
答案 1 :(得分:0)
根据How to convert array to SimpleXML,只需:
$xml = new SimpleXMLElement('<phone/>');
array_walk_recursive($output, array ($xml, 'addChild'));
然后从
获取你的xml$xml->asXML();