PHP - 如何根据值将项目分配到两个数组中

时间:2015-03-06 05:47:59

标签: php arrays multidimensional-array shopping-cart

我有这个数组,

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Item 1
            [price] => 10
            [weight] => 200
        )

    [1] => Array
        (
            [id] => 2
            [name] => Item 2
            [price] => 100
            [weight] => 20
        )

    [2] => Array
        (
            [id] => 3
            [name] => Item 3
            [price] => 30
            [weight] => 300
        )

    [3] => Array
        (
            [id] => 4
            [name] => Item 4
            [price] => 20
            [weight] => 500
        )

    [4] => Array
        (
            [id] => 6
            [name] => Item 6
            [price] => 40
            [weight] => 10
        )

    [5] => Array
        (
            [id] => 7
            [name] => Item 7
            [price] => 200
            [weight] => 10
        )

)

我想把它分成两部分,所以我得到了这个结果,

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Item 1
                    [price] => 10
                    [weight] => 200
                )

            [1] => Array
                (
                    [id] => 3
                    [name] => Item 3
                    [price] => 30
                    [weight] => 300
                )

            [2] => Array
                (
                    [id] => 7
                    [name] => Item 7
                    [price] => 200
                    [weight] => 10
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [name] => Item 4
                    [price] => 20
                    [weight] => 500
                )

            [1] => Array
                (
                    [id] => 6
                    [name] => Item 6
                    [price] => 40
                    [weight] => 10
                )

            [2] => Array
                (
                    [id] => 2
                    [name] => Item 2
                    [price] => 100
                    [weight] => 20
                )

        )

)

有什么想法吗?

这很棘手的是每个包装的总价格不得超过250.而每个包装的总重量必须尽可能均匀(即使认为不可能)。你可以在这张照片中看到我的意思,

name     weight  price
                          package 1
Item 1   200     10
Item 3   300     30
Item 7   10      200
(total)  510     240

                          package 2    
Item 4   500     20
Item 6   10      40
Item 2   20      100
(total)  530     160

因此可以移动这两个包之间的项目,以便总重量均匀分布&#39 ;?似乎没有“移动”就无法实现。他们手动。我不能想到通过自动程序实现目标的任何方式。你能吗?

2 个答案:

答案 0 :(得分:1)

按重量对数组进行排序

usort($array, function($a, $b) {return $a['weight']>$b['weight'];});

我们使用价格和重量计数器初始化结果表

$sum=Array(
    Array("weight" => 0, "price" => 0, "res" => Array()),
    Array("weight" => 0, "price" => 0, "res" => Array()));

然后我们循环数组

while ($obj=array_pop($array)) {

我们选择较低的结果数组

    $ind=($sum[0]['weight']<$sum[1]['weight']) ? 0 : 1;

除非已达到最高金额

    if ($sum[$ind]['price']+$obj['price']>250) {
            $ind=1-$ind;

            ## object will be skipped if it can't fit in the allowed amount
            if ($sum[$ind]['price']+$obj['price']>250) continue;
    }

然后我们填写

    array_push($sum[$ind]['res'], $obj);
    $sum[$ind]['weight']+=$obj['weight'];
    $sum[$ind]['price']+=$obj['price'];

}

print_r($sum);

按预期结果:

 Array
 (
     [0] => Array
         (
             [weight] => 510
             [price] => 240
             [res] => Array
                 (
                     [0] => Array
                         (
                             [id] => 3
                             [name] => Item 3
                             [price] => 30
                             [weight] => 300
                         )

                     [1] => Array
                         (
                             [id] => 1
                             [name] => Item 1
                             [price] => 10
                             [weight] => 200
                         )

                     [2] => Array
                         (
                             [id] => 7
                             [name] => Item 7
                             [price] => 200
                             [weight] => 10
                         )

                 )

         )

     [1] => Array
         (
             [weight] => 530
             [price] => 160
             [res] => Array
                 (
                     [0] => Array
                         (
                             [id] => 4
                             [name] => Item 4
                             [price] => 20
                             [weight] => 500
                         )

                     [1] => Array
                         (
                             [id] => 2
                             [name] => Item 2
                             [price] => 100
                             [weight] => 20
                         )

                     [2] => Array
                         (
                             [id] => 6
                             [name] => Item 6
                             [price] => 40
                             [weight] => 10
                         )

                 )

         )

 )

答案 1 :(得分:0)

它与PHP或数组无关。据我所知,你的问题是Multiple knapsack problem。所以它有很多数学(参见一些解决方案here),也许最简单的方法是以编程方式查询所有可能的变体。