我在使用类内的foreach内的数组时遇到问题。我得到警告..在类foreach中的数组
上线PHP的警告:()提供的foreach无效参数
foreach($this->recordOfDiscounts as $key => $discount)
有问题的功能是下面,
public function modify_price($price, $product_id) {
foreach($this->recordOfDiscounts as $key => $discount) {
foreach($discount['get_one_free'] as $id) {
if($id == $product_id){
if($discount['valid'] > 0) {
$price -= $discount['cost'];
$this->recordOfDiscounts[$key]['valid'] -= 1;
}
}
}
}
return $price;
}
我是PHP新手,但是我收集的是类范围($this->
)需要在一个类中为var加上前缀。
error_log(print_r($this->recordOfDiscounts),0);
输出正确的数组信息,所以我知道它的定义。
Array
(
[0] => Array
(
[valid] => 1
[buy_one] => 2351
[cost] => 20
[old_cost] => 20
[get_one_free] => Array
(
[0] => 2471
[1] => 2470
[2] => 2472
[3] => 2473
[4] => 2474
[5] => 2475
[6] => 2476
[7] => 2477
)
)
)
你在哪里输出到error_log?尝试在modify_price函数的第一行输出'$ this-> recordOfDiscounts'的内容。 – shanethehat