繁体   English   中英

foreach 循环中的逗号分隔变量到 php 脚本中的 xml 行

[英]Comma-Separated variable in foreach loop to xml rows in php script

我正在尝试在 php 脚本中创建一个循环,该循环将 output xml 行。 这些值来自逗号分隔的变量,如下所示:

$commaValues = '5773270,5778216';
$lubuvnaCommas = explode(',', $commaValues);

foreach($lubuvnaCommas as $row){
        $expected = $row;
        $destinations = "<cl_id>".$expected."</cl_id>";
    }

目标变量应该在我的脚本中定义的 xml 结构中输出,如下所示:

$xml ='
   <?xml version="1.0" encoding="UTF-8"?>
   <destinations>
   '.$destinations.'
   </destinations>;

这意味着 xml 数据的最终 output 应如下所示:

$xml ='
   <?xml version="1.0" encoding="UTF-8"?>
   <destinations>
   <cl_id>5773270</cl_id>
   <cl_id>5778216</cl_id>
   </destinations>';

我尝试了很多选项,我觉得我的循环中缺少 output 数据循环 xml 数据。

工作正常:

$commaValues = '5773270,5778216';
$lubuvnaCommas = explode(',', $commaValues);
$destinations = '';

foreach($lubuvnaCommas as $row){
    // Concatenate current value with previous ones
    $destinations .= "<cl_id>".$row."</cl_id>";
}

$xml ='
   <?xml version="1.0" encoding="UTF-8"?>
   <destinations>
   '.$destinations.'
   </destinations>';

在这里拉小提琴。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM