PHP foreach()最后一个值没有被比较

时间:2014-06-27 01:49:10

标签: php arrays

我有一系列值:

Array
(
[0] => Array
    (
        [product_colour_sizes_id] => 37105
        [product_colour_sizes_product_id] => 810
        [product_colour_sizes_option_value_colour_id] => 61
        [product_colour_sizes_option_value_size_id] => 904
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 16-165
        [product_colour_sizes_name_colours] => Red
    )

[1] => Array
    (
        [product_colour_sizes_id] => 37104
        [product_colour_sizes_product_id] => 810
        [product_colour_sizes_option_value_colour_id] => 61
        [product_colour_sizes_option_value_size_id] => 905
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 17-175
        [product_colour_sizes_name_colours] => Red
    )

[2] => Array
    (
        [product_colour_sizes_id] => 37103
        [product_colour_sizes_product_id] => 810
        [product_colour_sizes_option_value_colour_id] => 61
        [product_colour_sizes_option_value_size_id] => 906
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 185-19
        [product_colour_sizes_name_colours] => Red
    )

[3] => Array
    (
        [product_colour_sizes_id] => 37102
        [product_colour_sizes_product_id] => 810
        [product_colour_sizes_option_value_colour_id] => 61
        [product_colour_sizes_option_value_size_id] => 907
        [quantity] => 999
        [product_colour_sizes_name_sizes] => 19.5  
        [product_colour_sizes_name_colours] => Red
    )

)

然后我有一个foreach循环,它迭代上面的数据并将每个数组减少到对应于不同[product_colour_sizes_name_sizes]的单个行。但是我有一个问题,即没有比较最后一个数组。这是我的循环:

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

if($key > -1){
    if($product_combinations[$key+1]['product_colour_sizes_name_colours']
        == $product_combinations[$key]['product_colour_sizes_name_colours']
      ){

        $color  = $product_combinations[$key]['product_colour_sizes_name_colours'];
        $color['name'] = $color;
        $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$x"] =$product_combination['product_colour_sizes_option_value_size_id'];
        $x++;
    } else {
        array_push($colours, 
        array(
            $color => $sizes

        ));

        unset($sizes);
        $x = 0;
    }
}
}

基本上现在,我的第四个([3])数组被比较为 $product_combinations[$key+1] == $product_combinations[$key]['product_colour_sizes_name_colours'] $key+1为空值。

我知道如上所述的问题,但是不能想出用我现在的逻辑克服它的好方法,有人能想到解决方案吗?

修改 我试图做的是将行减少到这个确切的数组结构(包括当前未添加的最后一个数组):

Array
(
[0] => Array
    (
        [Red] => Array
            (
                [904] => 16-165
                [quantity] => 999
                [product_colour_sizes_id] => 37103
                [product_colour_sizes_product_id] => 810
                [product_colour_sizes_option_value_colour_id] => 61
                [product_colour_sizes_option_value_size_id0] => 904
                [905] => 17-175
                [product_colour_sizes_option_value_size_id1] => 905
                [906] => 185-19
                [product_colour_sizes_option_value_size_id2] => 906
            )

    )

)

1 个答案:

答案 0 :(得分:1)

您的原始核心逻辑有点不清楚,但根据您的示例输出数据,我相信下面的解决方案应该有效。

关键是真正理解你最终所需的输出,因为只需使用关联数组的魔力就可以消除原始代码所经历的许多逻辑循环。这解决了整个$key+1匹配$key问题 - 由于$key+1逻辑中的固有缺陷以及其他一些问题,它永远不会得到最后一个值。

无论如何,这里有一系列测试值,除了Blue之外还包括Red颜色的额外值;清理了代码:

// Test data array.
$product_combinations = array();

$product_combinations[0][product_colour_sizes_id] = 37105;
$product_combinations[0][product_colour_sizes_product_id] = 810;
$product_combinations[0][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[0][product_colour_sizes_option_value_size_id] = 904;
$product_combinations[0][quantity] = 999;
$product_combinations[0][product_colour_sizes_name_sizes] = '16-165';
$product_combinations[0][product_colour_sizes_name_colours] = 'Red';

$product_combinations[1][product_colour_sizes_id] = 37104;
$product_combinations[1][product_colour_sizes_product_id] = 810;
$product_combinations[1][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[1][product_colour_sizes_option_value_size_id] = 905;
$product_combinations[1][quantity] = 999;
$product_combinations[1][product_colour_sizes_name_sizes] = '17-175';
$product_combinations[1][product_colour_sizes_name_colours] = 'Red';

$product_combinations[2][product_colour_sizes_id] = 37103;
$product_combinations[2][product_colour_sizes_product_id] = 810;
$product_combinations[2][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[2][product_colour_sizes_option_value_size_id] = 906;
$product_combinations[2][quantity] = 999;
$product_combinations[2][product_colour_sizes_name_sizes] = '185-19';
$product_combinations[2][product_colour_sizes_name_colours] = 'Red';

$product_combinations[3][product_colour_sizes_id] = 37102;
$product_combinations[3][product_colour_sizes_product_id] = 810;
$product_combinations[3][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[3][product_colour_sizes_option_value_size_id] = 907;
$product_combinations[3][quantity] = 999;
$product_combinations[3][product_colour_sizes_name_sizes] = '19.5';
$product_combinations[3][product_colour_sizes_name_colours] = 'Red';

$product_combinations[4][product_colour_sizes_id] = 37102;
$product_combinations[4][product_colour_sizes_product_id] = 810;
$product_combinations[4][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[4][product_colour_sizes_option_value_size_id] = 907;
$product_combinations[4][quantity] = 999;
$product_combinations[4][product_colour_sizes_name_sizes] = '19.5';
$product_combinations[4][product_colour_sizes_name_colours] = 'Blue';

$product_combinations[5][product_colour_sizes_id] = 37103;
$product_combinations[5][product_colour_sizes_product_id] = 810;
$product_combinations[5][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[5][product_colour_sizes_option_value_size_id] = 906;
$product_combinations[5][quantity] = 999;
$product_combinations[5][product_colour_sizes_name_sizes] = '185-19';
$product_combinations[5][product_colour_sizes_name_colours] = 'Blue';

以下是基于原始海报代码和代码的重写代码逻辑。期望的输出:

// Init the colours array.
$colours = array();

// Init the sizes array.
$sizes = array();

// Init the increment array.
$increment = array();

// Main logic.
foreach ($product_combinations as $key => $product_combination){

  // Set the color & id.
  $color = $product_combinations[$key]['product_colour_sizes_name_colours'];
  $id = $product_combinations[$key]['product_colour_sizes_option_value_size_id'];

  // Set the sizes array data.
  $sizes[$color][$id] = $product_combinations[$key]['product_colour_sizes_name_sizes'];
  $sizes[$color]['quantity'] = $product_combinations[$key]['quantity'];
  $sizes[$color]['product_colour_sizes_id'] = $product_combination['product_colour_sizes_id'];
  $sizes[$color]['product_colour_sizes_product_id'] = $product_combination['product_colour_sizes_product_id'];
  $sizes[$color]['product_colour_sizes_option_value_colour_id'] = $product_combination['product_colour_sizes_option_value_colour_id'];
  $sizes[$color]['product_colour_sizes_option_value_size_id' . $increment[$color]] = $product_combination['product_colour_sizes_option_value_size_id'];
  $increment[$color]++;

  // Now place the colours array in the sizes array.
  $colours[$color] = $sizes[$color];

} // foreach

输出结果如下:

Array
(
    [Red] => Array
        (
            [904] => 16-165
            [quantity] => 999
            [product_colour_sizes_id] => 37102
            [product_colour_sizes_product_id] => 810
            [product_colour_sizes_option_value_colour_id] => 61
            [product_colour_sizes_option_value_size_id] => 904
            [905] => 17-175
            [product_colour_sizes_option_value_size_id1] => 905
            [906] => 185-19
            [product_colour_sizes_option_value_size_id2] => 906
            [907] => 19.5
            [product_colour_sizes_option_value_size_id3] => 907
        )

    [Blue] => Array
        (
            [907] => 19.5
            [quantity] => 999
            [product_colour_sizes_id] => 37103
            [product_colour_sizes_product_id] => 810
            [product_colour_sizes_option_value_colour_id] => 61
            [product_colour_sizes_option_value_size_id] => 907
            [906] => 185-19
            [product_colour_sizes_option_value_size_id1] => 906
        )

)