伙计这是完整的代码:
$response = $this->db->select('DATE(`created_at`) as `day`')->select('MIN(`paid_amount`) as `min_amount`')->select('MAX(`paid_amount`) as `max_amount`')->get('orders');
$nation = array();
foreach ($response->result() as $row)
{
print_r((array)$row);
array_push($nation, (array)$row);
}
我得到了print_r的输出,如下所示:
Array
(
[day] => 2013-03-24
[min_amount] => 2.00
[max_amount] => 2.00
[avg_amount] => 2.000000
[total_revenue] => 2.00
[total_orders] => 1
[total_subscriptions] => 0
[total_refunds] => 0
[refunded_amount] => 1.00
)
我使用了下面的forloop但它不是wrking。如何遍历这个数组:
foreach($nation as $key=>$value)
{
echo $key;
}
答案 0 :(得分:2)
试试这个:
foreach($nation as $key=>$value)
{
echo $key." = ".$value;
echo "<br />";
}
应输出:
day = 2013-03-24
min_amount = 2.00
max_amount = 2.00
avg_amount = 2.000000
total_revenue = 2.00
total_orders = 1
total_subscriptions = 0
total_refunds = 0
refunded_amount = 1.00
如果没有打印出来,则表示数组$nation
为空。您也可以通过
print_r($nation);
看到更新后的问题,这应该可以解决问题:
foreach($nation as $array)
{
echo $array['day'];
echo '<br />';
echo $array['min_amount'];
echo '<br />';
.
.
.
and so on
}
答案 1 :(得分:0)
不确定您是如何声明数组的,但请参见下文:
$nation = array (
'day' => 2013-03-24,
'min_amount' => 2.00,
'max_amount' => 2.00,
'avg_amount' => 2.000000,
'total_revenue' => 2.00,
'total_orders' => 1,
'total_subscriptions' => 0,
'total_refunds' => 0,
'refunded_amount' => 1.00,
);
foreach ($nation as $key=>$value) {
echo $key . '<br>';
}
答案 2 :(得分:0)
您的阵列看起来已经打印出来了。
尝试var_dump($nation);
并查看显示的内容 - 您的$nation
可能不是数组或未设置。
看起来应该是这样的:
$nation = array (
'day' => 2013-03-24,
'min_amount' => 2.00,
'max_amount' => 2.00,
'avg_amount' => 2.000000,
'total_revenue' => 2.00,
'total_orders' => 1,
'total_subscriptions' => 0,
'total_refunds' => 0,
'refunded_amount' => 1.00
);
打印出来:
foreach($nation as $key => $value){
echo $key.' -> '.$value.'<br/>';
}
答案 3 :(得分:0)
您正在使用array_push()将行输入$ nation。每行将从0,1,2等编号。如果您只有1行,则只有项0
。那是你看到的0
。
你实际上需要这样做:
foreach($nation as $row) // getting the rows you entered with array_push(). In your case now it's just 1.
{
foreach($row as $key=>$value) // getting the fields in each row
{
echo $key; // getting the field name. (Use $value if you want the field data.)
}
}