我有一个购物篮,例如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值分开的因素。例如,在此购物篮中,我们有shopid=4
中的2件商品和shopid=6
中的1商品以及shopid=5
中的一件商品,如何为每个商店创建单独的商品。
首先我使用usort函数对shopid进行排序数组但是我不能继续为每个相同而不是相同的项目商店创建因子
答案 0 :(得分: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'
)
)
)
答案 1 :(得分: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
)
)
)
答案 2 :(得分: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>";