循环错误,没有值

时间:2018-02-13 02:41:06

标签: php

我的循环不起作用并发出此错误:

  

foreach()

提供的参数无效

有问题的元素是$option['products_option_value']

$options_array的价值:

array (size=4)
  0 => 
    array (size=7)
      'products_option_id' => string '406' (length=3)
      'option_id' => string '42' (length=2)
      'name' => string 'Couleur' (length=7)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=13)
          'products_option_value_id' => string '279' (length=3)
          'option_value_id' => string '186' (length=3)
          'name' => string 'Vert' (length=4)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
  1 => 
    array (size=7)
      'products_option_id' => string '405' (length=3)
      'option_id' => string '42' (length=2)
      'name' => string 'Couleur' (length=7)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=13)
          'products_option_value_id' => string '278' (length=3)
          'option_value_id' => string '187' (length=3)
          'name' => string 'Red' (length=3)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
  2 => 
    array (size=7)
      'products_option_id' => string '402' (length=3)
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=13)
          'products_option_value_id' => string '275' (length=3)
          'option_value_id' => string '180' (length=3)
          'name' => string 'M' (length=1)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '20.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
  3 => 
    array (size=7)
      'products_option_id' => string '401' (length=3)
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=13)
          'products_option_value_id' => string '274' (length=3)
          'option_value_id' => string '180' (length=3)
          'name' => string 'M' (length=1)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)

我的代码

  $data['options'] = [];

  $options_array = $this->ProductsAttributesShop->getProductOptions($Products->getID());

  foreach ($options_array as $option) {
    $product_option_value_data = [];
      foreach ($option['products_option_value'] as $option_value) {
        if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
           $product_option_value_data[] =  ['products_option_value_id' => $option_value['products_option_value_id'],
                                           'option_value_id'         => $option_value['option_value_id'],
                                           'name'                    => $option_value['name'],
                                           'image'                   => $option_value['image'],
                                           'quantity'                => $option_value['quantity'],
                                           'price'                   => $option_value['price'],
                                           'price_prefix'            => $option_value['price_prefix'],
                                           'products_option_model'   => $option_value['products_option_model'],
                                          ];
        }
      }

    $data['options'][] = [
                          'product_option_id'    => $option['product_option_id'],
                          'products_option_value' => $product_option_value_data,
                          'option_id'            => $option['option_id'],
                          'name'                 => $option['name'],
                          'type'                 => $option['type'],
                          'value'                => $option['value'],
                          'required'             => $option['required']
                          ];
  }

1 个答案:

答案 0 :(得分:0)

您应该使用isset检查内部

    foreach ($options_array as $option) {
        $product_option_value_data = [];
          foreach ($option['products_option_value'] as $option_value) {
// Change below IF 
            if ((isset($option_value['subtract']) && !empty($option_value['subtract'])) || ($option_value['quantity'] > 0)) {
               $product_option_value_data[] =  ['products_option_value_id' => $option_value['products_option_value_id'],
                                               'option_value_id'         => $option_value['option_value_id'],
                                               'name'                    => $option_value['name'],
                                               'image'                   => $option_value['image'],
                                               'quantity'                => $option_value['quantity'],
                                               'price'                   => $option_value['price'],
                                               'price_prefix'            => $option_value['price_prefix'],
                                               'products_option_model'   => $option_value['products_option_model'],
                                              ];
            }
          }