php - Final element in foreach() not be compared -
i have given array such:
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 ) )
i have foreach()
loop loops on above data , reduces down single array. loop here:
foreach ($product_combinations $key => $product_combination){ // set offiset key. $offset_key = $key + 1; if(array_key_exists($offset_key, $product_combinations)){ echo 'yes' . '<br>'; } // check if 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 sizes color array. $colours[$color] = $sizes; } // if (==) } // foreach
the problem loop not compare last indexed array. i've tried several iterations of logic no avail. last array not looping over.
[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 )
the outcome looping on data gives me result:
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 ) )
however, want this, last element compared , included:
[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 ) )
your problem lies here:
// set offiset key. $offset_key = $key + 1; if(array_key_exists($offset_key, $product_combinations)){ echo 'yes' . '<br>'; }
just yo start looping, add +1 $key, starting array position 1 instead of position 0 (your code search n+1 position, 1 greater max array elements).
you need change logic here.
Comments
Post a Comment