I am trying to get PHP to adjust some array values so that in all instances of objects being passed through the functionality, it will compare length width and height of those array values, and align them as best as possible.
Imagine, 3 cigarette cartons. They are exactly the same size, shape and volume, but one is standing up, one is lying flat, and the other is on its short side.
Then, imagine a 4th cigarette carton. Its the same length and width as the other packets however its has more cigarette packets inside the container, therefore making its height (or width/length dependent on user input) to the other packets.
The function should still try to orientate the packet into the original packet in the array.
The example of cigarette cartons is not the real application. I have a bunch of boxes that need sorting into common arrangements so that they can be stacked. It should be smart enough to either compute, or ignore any additional shapes that relate to objects of similar size in at least 2 measurements. If all 3 are the same sizes but different orientation it should correct them where applicable, or break out of the loop if incomparable.
I have mocked up some sample PHP code, which should give some good test cases and expected results.
Any help is super appreciated, I have tried alot of combinations of array_diffs and array_intersects but seem to be coming up short on 1 solution for all scenarios.
UPDATE:
I think i am starting to come to the realization that there are 3 categories of calculation. After implementation they might be able to be refactored.
If you think about the first object that will not be rotated, then it has three possibilities and I may need to code separate logic for each to apply for each loop underneath it. Maybe the logic for case 1 and 2 will be somewhat similar......
- AxBxC ALL DIFFERENT SIZES, COMPARE
- AxBxB 2 SIMILAR SIZES, COMPARE
- AxAxA ALL SAME SIZES, NO ROTATION
so its at least eliminating some factor... still working through it...
<?php
$params['object'] = array();
/* START TEST 1 */
$params['object'][] = array( 'height'=>'30',
'width'=>'10',
'length'=>'20');
$params['object'][] = array( 'height'=>'20',
'width'=>'30',
'length'=>'30');
/* END TEST 1 */
/* START TEST 2
$params['object'][] = array( 'height'=>'30',
'width'=>'30',
'length'=>'20');
$params['object'][] = array( 'height'=>'30',
'width'=>'20',
'length'=>'30');
/* END TEST 2 */
/* START TEST 3
$params['object'][] = array( 'height'=>'30',
'width'=>'16',
'length'=>'14');
$params['object'][] = array( 'height'=>'30',
'width'=>'20',
'length'=>'30');
/* END TEST 3 */
echo '<PRE>';
echo 'INPUT<BR>';
print_r($params['object']);
//Check for common orientations
/* PLEASE KEEP BETWEEN HERE --- */
$rotated = array();
foreach($params['object'] as $on=>$outer){
if(in_array($on,$rotated)){ continue; }
$rotated[$on] = $on;
if($oo['width']==$oo['length'] && $oo['length']==$oo['height']){ continue; }
$oo = array('width'=>$outer['width'],
'length'=>$outer['length'],
'height'=>$outer['height']);
foreach($params['object'] as $in=>$inner){
if(in_array($in,$rotated)){ continue; }
if($in==$on){ continue; }
$ii = array('width'=>$inner['width'],
'length'=>$inner['length'],
'height'=>$inner['height']);
/* --- TO HERE */
//DETERMINE HOW TO ROTATE THESE???
//IF ROTATED, MARK AS ROTATED[$in] TO PREVENT FURTHER ROTATIONS TO OCCUR TO THIS ITEM
$rotated[$in] = $in;
}
}
echo 'OUTPUT<BR>';
print_r($params['object']);
/* TEST 1 OUTPUT SHOULD BE - ALIGNED SAME ORIENTATION, BUT EACH OBJ IS DIF WIDTH
- same logic should work if any of the H/W/L are in different orders but values consistant in any 2 pairs
Array
(
[0] => Array
(
[height] => 30
[width] => 10
[length] => 20
)
[1] => Array
(
[height] => 30
[width] => 30
[length] => 20
)
)
*/
/* TEST 2 OUTPUT SHOULD BE - SAME VALUES, SO ALIGN COMPLETELY
- same logic should work if any of the H/W/L are in different orders
Array
(
[0] => Array
(
[height] => 30
[width] => 30
[length] => 20
)
[1] => Array
(
[height] => 30
[width] => 30
[length] => 20
)
)
*/
/* TEST 3 OUTPUT SHOULD BE - NO CHANGE BECAUSE LESS THAN 2 COMPATIBLE MEASUREMENTS, NO ADD TO $rotation[$in]
Array
(
[0] => Array
(
[height] => 30
[width] => 16
[length] => 14
)
[1] => Array
(
[height] => 30
[width] => 20
[length] => 30
)
)
*/
?>