foreach()中的最终元素不予比较

时间:2014-06-27 10:16:11

标签: php arrays

我有一个给定的数组:

Array
(
[0] => Array
    (
        [product_colour_sizes_id] => 31822
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 297
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 6     
        [product_colour_sizes_name_colours] => Grey
    )

[1] => Array
    (
        [product_colour_sizes_id] => 31823
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 295
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 7     
        [product_colour_sizes_name_colours] => Grey
    )

[2] => Array
    (
        [product_colour_sizes_id] => 31824
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 242
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 8     
        [product_colour_sizes_name_colours] => Grey
    )

[3] => Array
    (
        [product_colour_sizes_id] => 31825
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 296
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 9     
        [product_colour_sizes_name_colours] => Grey
    )

[4] => Array
    (
        [product_colour_sizes_id] => 31826
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 243
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 10    
        [product_colour_sizes_name_colours] => Grey
    )

)

然后我有一个foreach()循环,循环上面的数据并减少到一个数组。执行此操作的循环在此处:

foreach ($product_combinations as $key => $product_combination){

    // Set the offiset key.
    $offset_key = $key + 1;
    if(array_key_exists($offset_key, $product_combinations)){
        echo 'Yes' . '<br>';
    }

    // Check if the array key exists.
    if (array_key_exists($offset_key, $product_combinations)) {

        // Core logic.
        if ($product_combinations[$offset_key]['product_colour_sizes_name_colours'] == $product_combinations[$key]['product_colour_sizes_name_colours']) {

            $color = $product_combinations[$key]['product_colour_sizes_name_colours'];
            $id = $product_combinations[$key]['product_colour_sizes_option_value_size_id'];
            $sizes[$id] = $product_combinations[$key]['product_colour_sizes_name_sizes'];
            $quantity = $product_combinations[$key]['quantity'];
            $sizes['quantity'] = $quantity;

            /* Dont forget theses*/
            $sizes['product_colour_sizes_id'] = $product_combination['product_colour_sizes_id'];
            $sizes['product_colour_sizes_product_id'] = $product_combination['product_colour_sizes_product_id'];
            $sizes['product_colour_sizes_option_value_colour_id'] = $product_combination['product_colour_sizes_option_value_colour_id'];
            $sizes['product_colour_sizes_option_value_size_id' . $id_increment] =$product_combination['product_colour_sizes_option_value_size_id'];
            $id_increment++;
        }

        // Add the sizes to the color array.
        $colours[$color] = $sizes;

    } // if (==)

} // foreach

问题是此循环不会比较最后一个索引数组。我尝试了几次这种逻辑的迭代无济于事。这是它没有循环的最后一个数组。

[4] => Array
    (
        [product_colour_sizes_id] => 31826
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 243
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 10    
        [product_colour_sizes_name_colours] => Grey
    )

循环数据的结果目前给出了我的结果:

Array
(
[Grey] => Array
    (
        [297] => 6     
        [quantity] => 999
        [product_colour_sizes_id] => 31825
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 297
        [295] => 7     
        [product_colour_sizes_option_value_size_id1] => 295
        [242] => 8     
        [product_colour_sizes_option_value_size_id2] => 242
        [296] => 9     
        [product_colour_sizes_option_value_size_id3] => 296
    )

)

然而,我希望它看起来像这样,最后一个元素被比较和包括:

[Grey] => Array
    (     
        [quantity] => 999
        [product_colour_sizes_id] => 31825
        [product_colour_sizes_product_id] => 1495
        [product_colour_sizes_option_value_colour_id] => 377
        [product_colour_sizes_option_value_size_id] => 297
        [295] => 7     
        [product_colour_sizes_option_value_size_id1] => 295
        [242] => 8     
        [product_colour_sizes_option_value_size_id2] => 242
        [296] => 9     
        [product_colour_sizes_option_value_size_id3] => 296
        [297] => 6
        [product_colour_sizes_option_value_size_id4] => 243
        [243] => 10

    )

)

1 个答案:

答案 0 :(得分:1)

你的问题在于:

  // Set the offiset key.
   $offset_key = $key + 1;
      if(array_key_exists($offset_key, $product_combinations)){
          echo 'Yes' . '<br>';
      }

只要您开始循环,就会在$ key中添加+1,从位置1而不是位置0(您的代码搜索n + 1位置,因此比最大数组元素大1)有效地启动数组

你需要在这里改变你的逻辑。