php 行的分组数组并填充最早的组而不超出容量

b5lpy0ml  于 2023-01-04  发布在  PHP
关注(0)|答案(2)|浏览(124)

我有一个2d的“包”数组,它们有自己指定的厘米大小。我需要将这些包优化分配到一个新的3d数组中,以表示每个最大容量为265cm的包托盘。
Current code,用于一维数组大小:

$default_cc_height_fa = 265; // size of the pallet in cm
$sh_array = [50, 45, 30, 60, 70, 80]; // array of packs

// sort the array of packs in decreasing order of size
usort($sh_array, function($a, $b) {
    return $b - $a;
});

// initialize the array of pallets
$mix_cc_array = [];

// iterate through the array of packs
foreach ($sh_array as $pack) {
    // try to fit the pack into an existing pallet
    $packed = false;
    foreach ($mix_cc_array as &$pallet) {
        if ($pack <= $default_cc_height_fa - array_sum($pallet)) {
            $pallet[] = $pack;
            $packed = true;
            break;
        }
    }
    // if the pack does not fit into any existing pallet, create a new one
    if (!$packed) {
        $mix_cc_array[] = [$pack];
    }
}

print_r($mix_cc_array);

如何调整上面的代码以适应这样的2d数组:

$sh_array = [
    ['id_product' => 13, 'size' => 50],
    ['id_product' => 13, 'size' => 45],
    ['id_product' => 13, 'size' => 30],
    ['id_product' => 13, 'size' => 60],
    ['id_product' => 13, 'size' => 70],
    ['id_product' => 13, 'size' => 80]
];
// array of array of packs

当然,我需要管理大小,但我需要将整行数据(包)推入子数组(托盘)。

o0lyfsai

o0lyfsai1#

只需稍加修改,我们就可以循环遍历一个同时具有idsize属性的包数组,为了比较当前pack的大小和最大高度常量,我们还可以向pack添加属性以累加所有内容。
最后,因为我对退出两个循环都感兴趣,所以我使用了continue 2,尽管您也可以保留原来的$packed = false;方法。

<?php
const DEFAULT_CC_HEIGHT_FA = 265; // size of the pallet in cm

// define array of products to add
$sh_array = [["id_product" => 13, "size" => 50],
            ["id_product" => 13, "size" => 45],
            ["id_product" => 13, "size" => 30],
            ["id_product" => 13, "size" => 60],
            ["id_product" => 13, "size" => 70],
            ["id_product" => 13, "size" => 80]];

// sort the array of packs in decreasing order of size
usort($sh_array, function($a, $b) {
    return $b["size"] - $a["size"];
});

// initialize the array of pallets
$mix_cc_array = [];

// iterate through the array of packs
foreach ($sh_array as $listRow) {
    // try to fit the pack into an existing pallet
    foreach ($mix_cc_array as &$pallet) {

        $newHeight = $listRow["size"] + $pallet["totalHeight"];

        if ($newHeight <= DEFAULT_CC_HEIGHT_FA) {
            // add new pack to this pallet
            array_push($pallet["pallets"],$listRow);
            
            // update accumulators
            $pallet["palletsStored"] += 1;
            $pallet["totalHeight"] = $newHeight;

            // continue the outer loop
            continue 2;
        }
    }

    // if the pack does not fit into any existing pallet, create a new one
    $mix_cc_array[] = array("totalHeight"=> $listRow["size"],
        "palletsStored" => 1, "pallets" => [$listRow]);
}

print_r($mix_cc_array);
omvjsjqw

omvjsjqw2#

my earlier answer和这个答案的唯一区别是如何对数组数据进行排序和计数,算法是一样的。
代码:(Demo

$pallet_limit = 111;
$packs = [
    ['id_product' => 13, 'size' => 50],
    ['id_product' => 13, 'size' => 45],
    ['id_product' => 13, 'size' => 30],
    ['id_product' => 13, 'size' => 60],
    ['id_product' => 13, 'size' => 70],
    ['id_product' => 13, 'size' => 80]
];

usort($packs, fn($a, $b) => $b["size"] <=> $a["size"]);

$pallets = [];
foreach ($packs as $pack) {
    foreach ($pallets as &$pallet) {
        if ((array_sum(array_column($pallet, 'size')) + $pack["size"]) <= $pallet_limit) {
            $pallet[] = $pack;
            continue 2;
        }
    }
    $pallets[] = [$pack];
}
var_export($pallets);

1.按大小值降序排列行。
1.迭代您的产品阵列。
1.迭代一个新的数组,托盘将存储在其中。
1.如果给定的 Package “适合”所遇到的托盘,则将其推入该托盘;否则检查后续托盘;如果没有合适的托盘,创建一个新的托盘并将 Package 推入其中。

相关问题