将complexObjectArray转换为可用数组

问题描述:

I have a script which has output which looks like the following:

enter image description here

Which is something I've not experienced before. Normally the result would be the array and then the stdClass Object which I convert to an array using (array) $result. This one seems to be nested an extra 2 times however, so I have no idea how to access it so that I can turn each of those objects into an array.

So what I need to be able to achieve, for example, is that if I use the code echo $orders[0]->customer_id, the result would be 716'.

Could anybody please advise? My code is below if required. Thank you very much.

<?php
    $client= new SoapClient('*Magento URL*');
    $session_id = $client->login((object)array('username' => '*Magento Username*', 'apiKey' => '*Magento Password*'));

    try {
        $result = $client->salesOrderList((object)array('sessionId' => $session_id->result, 'filters' => null)); 
        $orders = (array) $result;

        echo '<pre>', print_r($orders), '</pre>';

    } catch (SoapFault $e) {
        echo 'Error: ', $e->getMessage(), '<hr>';
    }
?>

我的脚本输出如下所示: p>

p>

以前我没有经历过的事情。 通常结果是数组,然后是 stdClass Object code>,我使用(array)$ result code>将其转换为数组。 这个似乎嵌套了2次,所以我不知道如何访问它,以便我可以将每个对象变成一个数组。 p>

所以我需要的 例如,能够实现的是,如果我使用代码 echo $ orders [0] - &gt; customer_id code>,结果将是 716 code>'。 p >

请问有人可以提供建议吗? 如果需要,我的代码如下。 非常感谢。 p>

 &lt;?php 
 $ client = new SoapClient('* Magento URL *'); 
 $ session_id = $ client-&gt; login  ((object)array('username'=&gt;'* Magento Username *','apiKey'=&gt;'* Magento Password *')); 
 
尝试{
 $ result = $ client-&gt;  salesOrderList((object)array('sessionId'=&gt; $ session_id-&gt; result,'filters'=&gt; null));  
 $ orders =(array)$ result; 
 
 echo'&lt; pre&gt;',print_r($ orders),'&lt; / pre&gt;'; 
 
}} catch(SoapFault $ e){\  n echo'错误:',$ e-&gt; getMessage(),'&lt; hr&gt;'; 
} 
?&gt; 
  code>  pre> 
  div>

$result is an object with a result property which is an object with a complexObjectArray property that is an integer indexed array of objects. So:

$orders = $result->result->complexObjectArray;

Then:

echo $orders[0]->customer_id;

If there is more than one item in the array then you would need to loop over it. If there is only ever one then include the 0 when defining $orders:

$orders = $result->result->complexObjectArray[0];

Then:

echo $orders->customer_id;