简体   繁体   中英

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. 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 version="1.0" encoding="UTF-8"?>
   <destinations>
   '.$destinations.'
   </destinations>;

this means the final output of the xml data should look like following:

$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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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