dongzecai0684 2018-08-16 05:29
浏览 70

基于数组中的字符串合并文件

good day ,
so here is the idea , i have a bunch of XML links and i want to merge them into one XML and convert it into CSV file, but since put it one by one in a textbox put a lot of effort, i decide to input it in a single textarea each links divided by line breaks. so if there were 10 links so it should be 10 line of links in the textarea as well
this is my code :

$str = $_POST['inp'];
$arr = explode("
", $str);

$array = array(); 
$cnt=0;
$ct=0;

foreach ($arr as $line) {
$array[] = $line;
$cnt++;
};

function mergeFile ( DOMDocument $target, $fileName )    {
$source = new DOMDocument();
$source->load($fileName);
foreach ( $source->getElementsByTagName("resValidateFakturPm") as $row )   {
$import = $target->importNode($row, true);
$target->documentElement->appendChild($import);
}
}

$target = new DOMDocument();
$target->loadXML('<?xml version="1.0" encoding="utf-8"?><bunch></bunch>');

for ($ct=0;$ct<count($array);$ct++){
     mergeFile($target, $array[$ct]);
}


$target->save("res.xml");

but there is one tiny problem , when i put 10 of links on the textarea it didn't resulted 10 rows in the CSV files instead it only resulted 1 row ,the last one. with 9 rows before resulted blank.

where's my code fault ?

  • 写回答

1条回答 默认 最新

  • dongtang5776 2018-08-16 14:33
    关注

    You give $target to the function and modify a local copy.

    1) Either change your method body to

    function mergeFile ( DOMDocument &$target, $fileName )

    or

    2) return $target in function mergeFile and

    for ($ct=0; $ct < count($array); $ct++)
    {
         $target = mergeFile($target, $array[$ct]);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料