douqiang6448 2013-12-09 15:08
浏览 56
已采纳

什么是访问xml对象中的数据的正确方法

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.

object(stdClass)#49 (1) {
["NewOrder"] => object(stdClass)#50 (12) {
["BTN"] => string(10) "XXXXXXXXXXXXXXXxx"
["PreOrderTransactionId"] => string(22) "XXXXXXXX"
["PartnerOrderId"] => string(17) "XXXXXXXXXXX"
["QwestOrderId"] => string(9) "N57395699"
["SalesCode"] => string(7) "XXXXXXXX"
["OrderStatus"] => string(7) "Pending"
["OrderStatusCode"] => string(4) "OPEN"
["OrderStatusSourceSystem"] => string(5) "CPLUS"
["OrderStatusMessage"] => string(0) ""
["OrderStatusDate"] => string(10) "2013-12-09"
["OrderStatusTime"] => string(8) "08:02:30"
["ProductFamily"] => array(3) {
  [0] => object(stdClass)#51 (2) {
    ["ProductFamilyName"] => string(7) "BUNDLE+"
    ["ProductLines"] => object(stdClass)#52 (3) {
      ["WTN"] => string(10) "3033689919"
      ["AppointmentDate"] => string(10) "2013-12-20"
      ["Products"] => object(stdClass)#53 (5) {
        ["ProductName"] => string(36) "INTERNET 100+ MBPS & HOME PHONE PLUS"
        ["Usoc"] => string(5) "BBBVC"
        ["Quantity"] => string(1) "1"
        ["Action"] => string(1) "I"
        ["Status"] => string(4) "OPEN"
      }
    }
  }
 }
}
}

I have tried the following but it didn't work:

if (!is_array($this->Orders->NewOrder)) {
$this->Order->NewOrder = array($this->Orders->NewOrder["NewOrder"]);

}
foreach ($this->Orders->NewOrder as $order){?>

I am getting the following error: Fatal error: Cannot use object of type stdClass as array in

  • 写回答

2条回答 默认 最新

  • dougai0138 2013-12-09 15:26
    关注

    I think your NewOrder is only an array if it contains more than one object. Use something like this before your loop:

    if (!is_array(yourObject["NewOrder"])) {
        yourObject["NewOrder"] = array(yourObject["NewOrder"]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100