php从旧数组生成新数组

php从旧数组生成新数组,php,arrays,Php,Arrays,我有一些情况下,我需要做一个分裂项目为我的货物从项目订单。 规则是每批货物的最大重量为5。 这是我订购的物品: $items = [ [ "sku" => "SKU-A", "name" => "Product A", "weight" => 7, "dimension&

我有一些情况下,我需要做一个分裂项目为我的货物从项目订单。 规则是每批货物的最大重量为5。 这是我订购的物品:

$items = [
    [
        "sku"       => "SKU-A",
        "name"      => "Product A",
        "weight"    => 7,
        "dimension" => "20x30x10"
    ],
    [
        "sku"       => "SKU-B",
        "name"      => "Product B",
        "weight"    => 4,
        "dimension" => "10x10x20"
    ],
];
在执行拆分后,我希望得到以下结果:

// will create new array
// limit weight per shipment max 5kg
$item1 = [
    [
        "sku"       => "SKU-A",
        "name"      => "Product A",
        "weight"    => 5,
        "dimension" => "20x30x10"
    ]
];

$item2 = [
    [
        "sku"       => "SKU-A",
        "name"      => "Product A",
        "weight"    => 2,
        "dimension" => "20x30x10"
    ], // this item from SKU-A where w => 7 - 5 = 2 
    [
        "sku"       => "SKU-B",
        "name"      => "Product B",
        "weight"    => 3,
        "dimension" => "10x10x20"
    ],
];

$item3 = [
    [
        "sku"       => "SKU-B",
        "name"      => "Product B",
        "weight"    => 1,
        "dimension" => "10x10x20"
    ],// this item from SKU-B where w => 7 - 5 = 2 
];

可能的方法是什么?谢谢。

@catLovers,我已经按照需要编写了此代码。。。请根据需要即兴创作/优化

$items = [
             [
                 "sku"       => "SKU-A",
                 "name"      => "Product A",
                 "weight"    => 7,
                 "dimension" => "20x30x10"
             ],
             [
                 "sku"       => "SKU-B",
                 "name"      => "Product B",
                 "weight"    => 4,
                 "dimension" => "10x10x20"
             ],
         ];
        
         $newItems = array();
         for ($x = 0; $x <= count($items)-1; $x++) {
          if ($items[$x]['weight'] > 5) {
             $weight = $items[$x]['weight'];
             $wt =5;
            do {
                $temp = array([
                 'sku'       => $items[$x]['sku'],
                 'name'      => $items[$x]['name'],
                 'weight'    => $wt,
                 'dimension' => $items[$x]['dimension']
                 ]);
                array_push($newItems,$temp);
                $weight=$weight-5;
                if ($weight <=5){ $wt=$weight;}
            
                } while ($weight <= 5);
             echo "<pre>";
             print_r($temp);
             echo "</pre>";
           }
           else {
           array_push($newItems,$items[$x]);
          
          }
         }
         echo "<pre>";
         print_r($newItems);
         echo "</pre>";
$items=[
[
“sku”=>“sku-A”,
“名称”=>“产品A”,
“重量”=>7,
“维度”=>“20x30x10”
],
[
“sku”=>“sku-B”,
“名称”=>“产品B”,
“重量”=>4,
“尺寸”=>“10x10x20”
],
];
$newItems=array();
对于($x=0;$x 5){
$weight=$items[$x]['weight'];
$wt=5;
做{
$temp=数组([
'sku'=>$items[$x]['sku'],
'name'=>$items[$x]['name'],
“重量”=>$wt,
'dimension'=>$items[$x]['dimension']
]);
数组推送($newItems,$temp);
$weight=$weight-5;

如果($weight)你尝试过什么吗?@Mathieu27我仍然不知道如何进行复杂循环,但进行调节器循环为什么要拆分SKU-B的重量为4,并且在规定的限制范围内为5?@Raky我只想插入SKU-a(限制后的重量=2)如果添加SKU-B将超过限制,则需要迭代
$items()
检查每个项目权重的值是否大于5,然后迭代,直到在新数组中为该项目创建新元素,使其在规定的5范围内。这种拆分没有现成的工具,但只需进行硬编码,但是,您可以动态分配值5。@catlowers,您需要确认选举和投票。