duan0708676887 2012-10-13 02:23
浏览 33
已采纳

如何在对象/数组上循环foreach

My DB returns an array of objects with arrays within.

Var_dump

array (size=2)
  0 => 
    object(stdClass)[22]
      public 'customer_id' => string '10' (length=2)
      public 'cart' => string 'a:1:{s:32:"f9bb1d342b1c2a0bfe982ef405369ec0";a:9:{s:5:"rowid";s:32:"f9bb1d342b1c2a0bfe982ef405369ec0";s:2:"id";s:9:"101_30524";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=761)
      public 'shipping_type' => string 'Ground' (length=6)
      public 'shipping_cost' => string '9.73' (length=4)
      public 'order_sub_total' => string '104.50' (length=6)
      public 'order_total' => string '114.23' (length=6)
      public 'id' => string '28' (length=2)
      public 'timestamp' => string '2012-10-12 20:10:30' (length=19)
  1 => 
    object(stdClass)[23]
      public 'customer_id' => string '10' (length=2)
      public 'cart' => string 'a:2:{s:32:"22d2d3d8584f6e0819c4e46af4d2fda2";a:9:{s:5:"rowid";s:32:"22d2d3d8584f6e0819c4e46af4d2fda2";s:2:"id";s:9:"101_94980";s:3:"qty";s:1:"1";s:5:"price";s:2:"64";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:164:"'... (length=1506)
      public 'shipping_type' => string 'Ground' (length=6)
      public 'shipping_cost' => string '19.46' (length=5)
      public 'order_sub_total' => string '148.25' (length=6)
      public 'order_total' => string '167.71' (length=6)
      public 'id' => string '29' (length=2)
      public 'timestamp' => string '2012-10-12 20:29:10' (length=19)

Notice cart is a multidimensional array. How do I loop through these objects and arrays and create a table?

<?php foreach($all_orders as $key => $val) : ?>

    <?php echo $key; ?>  <?php echo $val; ?>

<?php endforeach; ?>

This causes the following error: A PHP Error was encountered Severity: 4096 Message: Object of class stdClass could not be converted to string

  • 写回答

4条回答 默认 最新

  • du8864 2012-10-13 02:28
    关注

    Access the properties of the stdClass object using the -> operator:

    <?php foreach($all_orders as $key => $val) : ?>
    Customer ID <?php echo $val->customer_id ?> has a total of <?php echo $val->order_total ?><br />
    <?php endforeach ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动