我的DB
返回一个包含数组的对象数组。
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的对象无法转换为字符串
答案 0 :(得分:2)
你可以尝试
echo "<pre>";
foreach ( $cart as $all_orders ) {
foreach ( $all_orders as $key => $value ) {
echo $key, " = ", $value, PHP_EOL;
}
}
答案 1 :(得分: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 ?>
答案 2 :(得分:0)
PHP 5允许使用foreach
循环遍历对象的 public 属性。
有关详细信息(和代码示例):http://php.net/manual/en/language.oop5.iterations.php
答案 3 :(得分:0)
我在MVC中使用PHP的ArrayObject:
us3.php.net/manual/en/class.arrayobject.php
$arrayobject = new ArrayObject($dataRows);
for ($iterator = $arrayobject->getIterator(); $iterator->valid(); $iterator->next()) {
}