Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自动态php代码的格式化XML_Php_Xml - Fatal编程技术网

来自动态php代码的格式化XML

来自动态php代码的格式化XML,php,xml,Php,Xml,我是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[$v

我是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格式如下

 <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>

..
..
..
..
..
..
..
..
..
..
..
..

我希望这会对你有所帮助

 $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);   


}
$xml\u student\u info=新的SimpleXMLElement(“”);
$tour\u xml\u path=设置xml文件路径;
数组到xml1($data\u数组,$xml\u学生信息,$tour\u xml\u路径);
函数数组到xml1($student\u info,&$xml\u student\u info,$file\u name){
foreach($key=>$value形式的学生信息){
if(是_数组($value)){
如果(!是数字($key)){
$subnode=$xml\u student\u info->addChild($key”);
数组到xml1($value、$subnode、$file\u name);
}
否则{
$subnode=$xml\u student\u info->addChild(“项$key”);
数组到xml1($value、$subnode、$file\u name);
}
}
否则{
$xml\u student\u info->addChild($key),htmlspecialchars($value));
}
}
$xml\u student\u info->asXML($file\u name);
}
根据,只需执行以下操作:

$xml = new SimpleXMLElement('<phone/>');
array_walk_recursive($output, array ($xml, 'addChild'));

这不是xml,它是一种看起来有点像xml的格式。@arkascha它的伪xml是错误的。我猜他想要一个实际的电话号码,比如说,在询问之前,你们有并没有用谷歌搜索数组到xml?(-1). 例如,查看一个非常简单的简短解决方案idenote:停止使用不推荐的
mysql.*
函数。使用或代替。这是一个好的PDO。
$xml->asXML();