Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 缺少标记的SimpleXML到CSV_Php_Xml_Csv_Simplexml_Fputcsv - Fatal编程技术网

Php 缺少标记的SimpleXML到CSV

Php 缺少标记的SimpleXML到CSV,php,xml,csv,simplexml,fputcsv,Php,Xml,Csv,Simplexml,Fputcsv,因此,我有一个XML文件,看起来类似于: <container> <example1>some text</example1> <example2>some text</example2> <example3>some text</example3> <example4>some text</example4> </container> <container> &l

因此,我有一个XML文件,看起来类似于:

<container>
<example1>some text</example1>
<example2>some text</example2>
<example3>some text</example3>
<example4>some text</example4>
</container>
<container>
<example1>some text</example1>
<example2>some text</example2>
<example4>some text</example4>
</container>
我不知道如何预定义我需要的标题并在CSV的正确列中插入正确的信息

非常感谢任何指点

谢谢


mike

如果您的第一个条目始终显示所有字段,那么就没有必要对每行的标题字段进行迭代,并查看当前行是否包含所有条目

foreach ($xml->container as $information) {
    $vars = get_object_vars($information);
    $line = array();
    foreach($headers as $field) {
        $line[] = isset($vars[$field]) ? $vars[$field] : '';
    }
    fputcsv($f, $line, ',', '"');
}
foreach ($xml->container as $information) {
    $vars = get_object_vars($information);
    $line = array();
    foreach($headers as $field) {
        $line[] = isset($vars[$field]) ? $vars[$field] : '';
    }
    fputcsv($f, $line, ',', '"');
}