1

MyDB返回一个对象数组,其中包含数组。

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)

注意cart是一个多维数组。如何循环遍历这些对象和数组并创建一个表?

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

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

<?php endforeach; ?>

这会导致以下错误:遇到 PHP 错误严重性:4096 消息:stdClass 类的对象无法转换为字符串

4

4 回答 4

2

你可以试试

echo "<pre>";
foreach ( $cart as  $all_orders ) {
    foreach ( $all_orders as $key => $value ) {
        echo $key, " = ", $value, PHP_EOL;
    }
}
于 2012-10-13T02:25:05.363 回答
2

stdClass使用操作符访问对象的属性->

<?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 ?>
于 2012-10-13T02:28:02.587 回答
0

PHP 5 允许使用循环遍历对象的公共属性foreach

更多信息(和代码示例): http: //php.net/manual/en/language.oop5.iterations.php

于 2012-10-13T02:26:45.443 回答
0

我在我的 MVC 中使用 PHP 的 ArrayObject:
us3.php.net/manual/en/class.arrayobject.php

        $arrayobject = new ArrayObject($dataRows);
        for ($iterator = $arrayobject->getIterator(); $iterator->valid(); $iterator->next()) {

        }
于 2012-10-13T03:16:04.430 回答