0

I am creating XML data that creates a list of phone numbers that will be passed to an API. I have constructed a "hardcoded" XML structure that works.

$data = <<<DATA
                <Order>
                <CustomerOrderId>1234567890</CustomerOrderId>
                <Name>Test</Name>
                <ExistingTelephoneNumberOrderType>
                    <TelephoneNumberList>
                        <TelephoneNumber>1234567890</TelephoneNumber>
                        <TelephoneNumber>1234567891</TelephoneNumber>
                        <TelephoneNumber>1234567892</TelephoneNumber>
                        <TelephoneNumber>1234567893</TelephoneNumber>
                    </TelephoneNumberList>
                </ExistingTelephoneNumberOrderType>
                <SiteId>0001</SiteId>                      
            </Order> 
DATA;

... But now need to add multiple entries getting the list of phone numbers from an array.

$phone_numbers = array("1234567890", "1234567891", "1234567892", "1234567893");
$data = <<<DATA
                <Order>
                <CustomerOrderId>1234567890</CustomerOrderId>
                <Name>Test</Name>
                <ExistingTelephoneNumberOrderType>
                    <TelephoneNumberList>

                       //this is where i need the foreach data looped
                        foreach($phone_numbers as $value){
                        echo '<TelephoneNumber>' . $value . '</TelephoneNumber>';
                        }

                    </TelephoneNumberList>
                </ExistingTelephoneNumberOrderType>
                <SiteId>0001</SiteId>                      
            </Order>
DATA;
3
  • What programming language is that? Commented Apr 3, 2021 at 22:57
  • Always include a language tag on your questions, that will ensure they reach the widest possible audience Commented Apr 3, 2021 at 23:38
  • Sorry about that. I'm a newbie. Commented Apr 4, 2021 at 0:23

1 Answer 1

1

Your code is essentially correct; you just need to break your XML data in the middle and then add the phone numbers to it in the foreach loop before outputting the rest of the XML:

$phone_numbers = array("1234567890", "1234567891", "1234567892", "1234567893");
$data = <<<DATA
<Order>
    <CustomerOrderId>1234567890</CustomerOrderId>
    <Name>Test</Name>
    <ExistingTelephoneNumberOrderType>
        <TelephoneNumberList>

DATA;
foreach($phone_numbers as $value){
    $data .= "\t    <TelephoneNumber>$value</TelephoneNumber>\n";
}
$data .= <<<DATA
        </TelephoneNumberList>
    </ExistingTelephoneNumberOrderType>
    <SiteId>0001</SiteId>                      
</Order>
DATA;
echo $data;

Output:

<Order>
    <CustomerOrderId>1234567890</CustomerOrderId>
    <Name>Test</Name>
    <ExistingTelephoneNumberOrderType>
        <TelephoneNumberList>
            <TelephoneNumber>1234567890</TelephoneNumber>
            <TelephoneNumber>1234567891</TelephoneNumber>
            <TelephoneNumber>1234567892</TelephoneNumber>
            <TelephoneNumber>1234567893</TelephoneNumber>
        </TelephoneNumberList>
    </ExistingTelephoneNumberOrderType>
    <SiteId>0001</SiteId>                      
</Order>

Demo on 3v4l.org

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

Comments

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.