Php 类内foreach中的数组
我在类中使用foreach中的数组时遇到问题。我得到了警告 PHP警告:为foreach()提供的参数无效 在线Php 类内foreach中的数组,php,Php,我在类中使用foreach中的数组时遇到问题。我得到了警告 PHP警告:为foreach()提供的参数无效 在线 foreach($this->recordOfDiscounts as $key => $discount) 下面是有问题的函数 public function modify_price( $price, $product_id ) { foreach($this->recordOfDiscounts as $key => $discount) {
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->
)需要在类中为变量添加前缀
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
)
)
)
其中一个foreach语句的第一部分有一些不可iterable
$this->recordOfDiscounts
不是数组,或者在外部foreach的一次迭代中,$discounts['get\u one\u free']
不可编辑。对不起,我已将问题编辑得更具体。警告出现在foreach行($this->recordOfDiscounts as$key=>$discounts)。您在哪里输出到错误日志?尝试在修改价格函数的第一行输出$this->recordOfDiscounts
的内容。