doubu7425 2017-01-18 19:56
浏览 53
已采纳

将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>';
    }
?>
  • 写回答

1条回答 默认 最新

  • douqujin2767 2017-01-18 20:37
    关注

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,