0

我有一个购物篮,例如 6 件商品。有些物品适用于一家商店,而另一种物品适用于一家甚至两家商店。

我想为每个商店创造因素。我能怎么做?我怎么知道篮子里的这些物品是给一个商店买的,或者是给 2 个或 3 个商店买的。如何将这些项目分开来考虑。

Array('customer' => Array('basket' => Array(
                    '9_2' => Array
                        (
                            "row" => "0",
                            'item' => 'cd',
                            'count' => '1',
                            'sale_start_date' => '1391-12-25 19:27:56',
                            'sale_end_date' => '1392-04-20 19:27:49',
                            'sale_price' => '40500',
                            'price' => '54564',
                            'id' => '999035',
                            'shopid' => '4'
                        ),
                        '999_17' => Array
                        (
                            'row' => '1',
                            'item' => 'car',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '6'
                        ),
                        '9_3' => Array
                        (
                            'row' => '2',
                            'item' => 'book',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '4'
                        ),
                        '10_5' => Array
                        (
                            'row' => '2',
                            'item' => 'dvd',
                            'count' => '1',
                            'sale_start_date' => '0000-00-00 00:00:00',
                            'sale_end_date' => '0000-00-00 00:00:00',
                            'sale_price' => '0',
                            'price' => '520000',
                            'id' => '999039',
                            'code' => 'b125nh',
                            'shopid' => '5'
                        )
                    )
                )
            );

这个例子是我的 SESSION 中的数组。我想要带有 shopid 值的单独因素。例如,在这个购物篮中,我们有 2 件商品shopid=4,还有 1 件商品shopid=6和一件商品,shopid=5如何为每个商店创建单独的因素。

首先,我使用 usort 函数对带有 shopid 的排序数组进行排序,但我无法继续将每个相同和不同的商品商店分开以创建因子

4

3 回答 3

0

像这样的东西应该工作

<?php

$shopItems = array();
foreach ($_SESSION['customer']['basket'] as $basket) {
    $key = $basket['shopid'];
    $shopItems[$key][] = $basket;
}

这会将来自相同商店 ID 的商品组存储在数组 $shopItems 的元素中

例如(仅显示商店 id 4)

Array (
    [4] => Array (
        [0] => array(
            "row" => "0",
            'item' => 'cd',
            'count' => '1',
            'sale_start_date' => '1391-12-25 19:27:56',
            'sale_end_date' => '1392-04-20 19:27:49',
            'sale_price' => '40500',
            'price' => '54564',
            'id' => '999035',
            'shopid' => '4'
        ),
        [1] => array(
            'row' => '2',
            'item' => 'book',
            'count' => '1',
            'sale_start_date' => '0000-00-00 00:00:00',
            'sale_end_date' => '0000-00-00 00:00:00',
            'sale_price' => '0',
            'price' => '520000',
            'id' => '999039',
            'code' => 'b125nh',
            'shopid' => '4'
        )
    )
)
于 2013-05-17T09:49:01.497 回答
0
$basket = array();
foreach ($_SESSION['customer']['basket'] as $k => $v)
    $basket[$v['shopid']][$k] = $v;
print_r($basket);

结果:

Array
(
    [4] => Array
        (
            [9_2] => Array
                (
                    [row] => 0
                    [item] => cd
                    [count] => 1
                    [sale_start_date] => 1391-12-25 19:27:56
                    [sale_end_date] => 1392-04-20 19:27:49
                    [sale_price] => 40500
                    [price] => 54564
                    [id] => 999035
                    [shopid] => 4
                )

            [9_3] => Array
                (
                    [row] => 2
                    [item] => book
                    [count] => 1
                    [sale_start_date] => 0000-00-00 00:00:00
                    [sale_end_date] => 0000-00-00 00:00:00
                    [sale_price] => 0
                    [price] => 520000
                    [id] => 999039
                    [code] => b125nh
                    [shopid] => 4
                )

        )

    [6] => Array
        (
            [999_17] => Array
                (
                    [row] => 1
                    [item] => car
                    [count] => 1
                    [sale_start_date] => 0000-00-00 00:00:00
                    [sale_end_date] => 0000-00-00 00:00:00
                    [sale_price] => 0
                    [price] => 520000
                    [id] => 999039
                    [code] => b125nh
                    [shopid] => 6
                )

        )

    [5] => Array
        (
            [10_5] => Array
                (
                    [row] => 2
                    [item] => dvd
                    [count] => 1
                    [sale_start_date] => 0000-00-00 00:00:00
                    [sale_end_date] => 0000-00-00 00:00:00
                    [sale_price] => 0
                    [price] => 520000
                    [id] => 999039
                    [code] => b125nh
                    [shopid] => 5
                )

        )

)
于 2013-05-17T09:50:48.220 回答
0

这应该可以解决问题。

function get_seperated_shops($all_items)
{
    $seperated_shops = array();
    foreach($all_items AS $item => $properties)
    {
        $seperated_shops[$properties['shopid']][$item] = $properties;
    }
    return $seperated_shops;
}

echo "<pre>";
print_r(get_seperated_shops($_SESSION['customer']['basket']));
echo "</pre>";
于 2013-05-17T09:51:42.000 回答