简体   繁体   中英

PHP SimpleXML Loop Question

I am trying to do a foreach using xPath. It's working fine and looping through the XML.

My question is can I somehow use a while statement with it. I basically need to find each value of a certain attribute but not for the entire XML. I want the foreach to stop once it hits a certain attribute.

Example:

<InvoiceRet>

    <TxnID>23827-1290392819</TxnID>
    <TimeCreated>2010-11-21T21:26:59-05:00</TimeCreated>

        <InvoiceLineRet>
            <TxnLineID>23829-1290392819</TxnLineID>
        </InvoiceLineRet>

        <InvoiceLineRet>
            <TxnLineID>2382A-1290392819</TxnLineID>
        </InvoiceLineRet>

</InvoiceRet>

<InvoiceRet>

    <TxnID>2382F-1290392820</TxnID>

        <InvoiceLineRet>
            <TxnLineID>2382B-1290392819</TxnLineID>
        </InvoiceLineRet>

                    <InvoiceLineRet>
            <TxnLineID>2382B-1290392819</TxnLineID>
        </InvoiceLineRet>

</InvoiceRet>

I basically need to loop through the first set of but not the second set. So I was hoping that I can stop the loop when it hits the second .

The problem is I have no idea how to do it.

Any help would be great.

Thanks!

If you're only looking to get the first one all the time then, you could just break the loob after getting the first one.

foreach ($TxnID as $t){
     echo $t;
     break;
}

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