0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i was stuck for many hours on this. now you solved it, appreciate it. Thanks dear
I know OP accepted this answer, but can you please echo $xml and post the output? For some reason, I'm not getting the expected output presented in the question.
Added a fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.