循环中的错误数据foreach确实显示了良好的语法

时间:2018-02-10 15:07:09

标签: php

我不明白为什么在foreach循环中我无法使用 例如$products_option_value['products_option_value_id']。我必须用     $option['products_option_value']['products_option_value_id']显示好结果,否则我只有一个值

元素下面

var_dump($options_array);
array (size=7)
  0 => 
    array (size=7)
      'products_option_id' => string '213' (length=3)
      'products_option_value' => 
        array (size=13)
          'products_option_value_id' => string '171' (length=3)
          'option_value_id' => string '179' (length=3)
          'name' => string 'S' (length=1)
          'image' => null
          'quantity' => string '100' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '1.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '99' (length=2)
          'products_option_model' => string '99' (length=2)
          'option_tax_class_id' => string '99' (length=2)
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
etc


  foreach ($options_array as $option) {
    foreach ($option['products_option_value'] as $products_option_value) {
      var_dump($products_option_value);
    }
  }

结果是:

   var_dump($option); result

    array (size=7)
      'products_option_id' => string '213' (length=3)
      'products_option_value' => 
        array (size=13)
          'products_option_value_id' => string '171' (length=3)
          'option_value_id' => string '179' (length=3)
          'name' => string 'S' (length=1)
          'image' => null
          'quantity' => string '100' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '1.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '99' (length=2)
          'products_option_model' => string '99' (length=2)
          'option_tax_class_id' => string '99' (length=2)
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null

var_dump($products_option_value) result

    products_info_options_new.php:84:string '171' (length=3)
    products_info_options_new.php:84:string '179' (length=3)
    products_info_options_new.php:84:string 'S' (length=1
    etc

1 个答案:

答案 0 :(得分:0)

http://php.net/manual/en/control-structures.foreach.php

在通过具有键的数组进行迭代时,您应该使用foreach($array as $key => $value)。 (我的意思是这样的数组:array("key" => $value),这也是你的数组)