Note that there are some explanatory texts on larger screens.

plurals
  1. POWhats the proper way to access the data in a xml object
    primarykey
    data
    text
    <p>I am working on a API project where i get an XML Object as a response. The response can contain one or more products in the NewOrder object(below).However when i try to display the info using a foreach loop it breaks if the only has one entry. i guess it is because the index [0] does not exist in the object.how can i through the xml object and display since there is no [0] i the object. OR how do i add the index [0] in the object.</p> <pre><code>object(stdClass)#49 (1) { ["NewOrder"] =&gt; object(stdClass)#50 (12) { ["BTN"] =&gt; string(10) "XXXXXXXXXXXXXXXxx" ["PreOrderTransactionId"] =&gt; string(22) "XXXXXXXX" ["PartnerOrderId"] =&gt; string(17) "XXXXXXXXXXX" ["QwestOrderId"] =&gt; string(9) "N57395699" ["SalesCode"] =&gt; string(7) "XXXXXXXX" ["OrderStatus"] =&gt; string(7) "Pending" ["OrderStatusCode"] =&gt; string(4) "OPEN" ["OrderStatusSourceSystem"] =&gt; string(5) "CPLUS" ["OrderStatusMessage"] =&gt; string(0) "" ["OrderStatusDate"] =&gt; string(10) "2013-12-09" ["OrderStatusTime"] =&gt; string(8) "08:02:30" ["ProductFamily"] =&gt; array(3) { [0] =&gt; object(stdClass)#51 (2) { ["ProductFamilyName"] =&gt; string(7) "BUNDLE+" ["ProductLines"] =&gt; object(stdClass)#52 (3) { ["WTN"] =&gt; string(10) "3033689919" ["AppointmentDate"] =&gt; string(10) "2013-12-20" ["Products"] =&gt; object(stdClass)#53 (5) { ["ProductName"] =&gt; string(36) "INTERNET 100+ MBPS &amp; HOME PHONE PLUS" ["Usoc"] =&gt; string(5) "BBBVC" ["Quantity"] =&gt; string(1) "1" ["Action"] =&gt; string(1) "I" ["Status"] =&gt; string(4) "OPEN" } } } } } } </code></pre> <p>I have tried the following but it didn't work:</p> <pre><code>if (!is_array($this-&gt;Orders-&gt;NewOrder)) { $this-&gt;Order-&gt;NewOrder = array($this-&gt;Orders-&gt;NewOrder["NewOrder"]); } foreach ($this-&gt;Orders-&gt;NewOrder as $order){?&gt; </code></pre> <p>I am getting the following error: Fatal error: Cannot use object of type stdClass as array in</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload