2013-01-09 79 views
0

我在使用类内的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 
       ) 

     ) 

) 
+0

你在哪里输出到error_log?尝试在modify_price函数的第一行输出'$ this-> recordOfDiscounts'的内容。 – shanethehat

回答

2

作为声明的第一部分,其中一个foreach语句有些不可迭代。

$this->recordOfDiscounts不是一个数组,或者在外部foreach的一次迭代中,$discount['get_one_free']是不可迭代的。

+0

对不起,我已经编辑了更具体的问题。警告发生在行foreach($ this-> recordOfDiscounts作为$ key => $折扣)。 – Cam