I have an array containing information about boxes, their volume and weight.
$boxes = array('box1' => array('v' => 1.2, 'w' => 2.45),
'box2' => array('v' => 3.1, 'w' => 1.37),
'bigbox1' => array('v' => 6.2, 'w' => 5.45),
'box3' => array('v' => 2.15, 'w' => 2.94),
'smallbox1' => array('v' => 0.86, 'w' => 1.04),
'bigbox2' => array('v' => 11.3, 'w' => 10.9), ..);
I'd like to eliminate all boxes that have larger volume and lower weight at the same time than any other. E. g. box2 will be eliminated, because box1 is smaller and heavier.
What you could do is use 2 foreach loops. For every box in the first foreach, you loop again over all the boxes and if your condition is met you can unset that from the $boxes
array.
$boxes = array(
'box1' => array('v' => 1.2, 'w' => 2.45),
'box2' => array('v' => 3.1, 'w' => 1.37),
'bigbox1' => array('v' => 6.2, 'w' => 5.45),
'box3' => array('v' => 2.15, 'w' => 2.94),
'smallbox1' => array('v' => 0.86, 'w' => 1.04),
'bigbox2' => array('v' => 11.3, 'w' => 10.9)
);
foreach ($boxes as $key => $box) {
foreach ($boxes as $b) {
if (($box['v'] > $b['v']) && ($box['w'] < $b['w'])) {
unset($boxes[$key]);
}
}
}