我正在考虑编写一个 PHP 函数,该函数使用一种方便的方法/公式来计算装载物品的包裹/托盘的尺寸。
这是一个包含项目的数组的示例。注意:有些物品被标记为单独包裹发送。有些物品可能不会倾斜。
$items = array(
1 => array(
'quantity' => 1,
'weight' = 1,
'dimensions' => array(80, 50, 50), // Length, Width, Height
'separate' => true, // If the item should be sent as a separate package
'tiltable' => false, // False if the item has a 'this side up' sticker
),
2 => array(
'quantity' => 3,
'weight' = 1,
'dimensions' => array(21, 15, 10),
'separate' => false,
'tiltable' => true,
),
3 => array(
'quantity' => 2,
'weight' = 1,
'dimensions' => array(18, 19, 20),
'separate' => false,
'tiltable' => true,
),
// ... and so on ...
);
有没有人对此有一点点知识或经验?我不想重新发明轮子。
我想到的功能是这样的:*(可能会出现语法错误)*
function build_packages($items, $max_weight=0, $max_length=0, $max_width=0, $max_height=0) {
$packages = array();
// Step through each item
foreach ($items as $item) {
// Twist and turn item. Longest side first ([0]=length, [1]=width, [2]=height)
if (!empty($item['tiltable'])) {
rsort($item['dimensions'], SORT_NUMERIC);
} else {
if ($item['dimensions'][0] < $item['dimensions'][1]) {
$item['dimensions'] = array($item['dimensions'][1], $item['dimensions'][0], $item['dimensions'][2]);
}
}
// Validate item
if (!empty($max_weight) && $item['weight'] > $max_weight) return false;
if (!empty($max_length) && $item[0] > $max_length) return false;
if (!empty($max_width) && $item[1] > $max_width) return false;
if (!empty($max_height) && $item[2] > $max_height) return false;
// Step through quantities
for ($i=0; $i<$item['quantity']; $i++) {
// Step through packages
$package_found = false;
foreach (array_keys($packages) as $key) {
// Skip to next package on certain conditions
if ($packages[$key]['separate']) continue;
// ...
// Do some logic
// ...
// Modify package
$package_found = true;
$packages[$key]['num_items']++;
$packages[$key]['weight'] += $item['weight'];
$packages[$key]['dimensions'] = array(0, 0, 0); // <--- Replace with new dimensions
// Twist and turn package. Longest side first ([0]=length, [1]=width, [2]=height)
if (!empty($item['tiltable'])) {
rsort($packages[$key]['dimensions'], SORT_NUMERIC);
} else {
if ($packages[$key]['dimensions'][0] < $packages[$key]['dimensions'][1]) {
$packages[$key]['dimensions'] = array($packages[$key]['dimensions'][1], $packages[$key]['dimensions'][0], $packages[$key]['dimensions'][2]);
}
}
break;
}
if ($package_found) continue;
// Add to a new package
$packages[] = array(
'num_items' => 1,
'weight' => $item['weight'],
'dimensions' => $item['dimensions'],
'separate' => $item['separate'],
'tiltable' => $item['tiltable'],
);
}
}
return $packages;
}
愿意帮助一些代码吗?