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