error on loop, does not have value

My loop doesn't work and gives this error:

Invalid argument supplied for foreach()

The element in question is $option['products_option_value']

value of $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)

my code

  $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 answer

  • answered 2018-02-13 02:41 AKZhang

    You should use isset to check inside

        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'],
                                                  ];
                }
              }