简体   繁体   English

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

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

I am trying to make a loop that will output xml rows in the php script.我正在尝试在 php 脚本中创建一个循环,该循环将 output xml 行。 The values come from comma separated variable like following:这些值来自逗号分隔的变量,如下所示:

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

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

the destination variable should be outputted in the defined xml structure in my script like following:目标变量应该在我的脚本中定义的 xml 结构中输出,如下所示:

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

this means the final output of the xml data should look like following:这意味着 xml 数据的最终 output 应如下所示:

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

I tried many options, i feel there is something missing in my loop that doesn't output a loop of xml data.我尝试了很多选项,我觉得我的循环中缺少 output 数据循环 xml 数据。

Works fine:工作正常:

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

Fiddle here .在这里拉小提琴。

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

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