6

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.

Visual Example

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......

  1. AxBxC ALL DIFFERENT SIZES, COMPARE
  2. AxBxB 2 SIMILAR SIZES, COMPARE
  3. 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
        )

)
*/

?>
6
  • This is an interesting question, although I have to admit the diagram is confusing me somewhat because it doesn't seem to match with your description of all the cigarette packets lying on different sides. Then again, it is one in the morning :D Commented Dec 14, 2015 at 1:05
  • I guess I could find an image of 3 different positioned cartons , but I tried to label them to demonstrate their orientation ( W / L / H). Maybe it is just 1am for you... haha. Maybe I should update that photo. Clearly my photoshop skills are elite. :P
    – DRT
    Commented Dec 14, 2015 at 1:18
  • All good. If you are stacking items by matching dimensions, are you stacking these within the bounds of some sort of parent container? Commented Dec 14, 2015 at 1:23
  • I am, but that is already calculated and outside the scope of the question I need answered. I just need to sort common orientations elegantly.
    – DRT
    Commented Dec 14, 2015 at 1:41
  • Updated original image to make it more clear, thanks Darragh.
    – DRT
    Commented Dec 14, 2015 at 1:54

0

Browse other questions tagged or ask your own question.