我下面的會話數組存儲購物車的物品:PHP會話陣列添加元素,如果同一ID
的print_r($ _ SESSION [ '項目']):
Array
(
[0] => Array
(
[p_name] => Product A
[p_price] => 13.90
[p_seller] => 2001
)
[1] => Array
(
[p_name] => Product B
[p_price] => 2.00
[p_seller] => 2002
)
[2] => Array
(
[p_name] => Product C
[p_price] => 1.20
[p_seller] => 2002
)
[3] => Array
(
[p_name] => Product D
[p_price] => 50.00
[p_seller] => 2001
)
)
我有一個循環從DB調用額外的數據:
while($res = $shipment->fetch_object()){
$seller_id = $res->seller;
$shipment_cost = $res->shipment_fee;
//array_push($_SESSION['items']['p_fee'], $shipment_cost);
}
讓說,在一個循環中,我得到以下數據:
$ seller_id = 2001; $ shipment_cost = 5.00
$ seller_id = 2002; $ shipment_cost = 3.00
我要分配並用名稱['p_fee']
添加一個額外的元件到其相應的組['p_seller']
如果$ seller_id(2001)== [ 'p_seller'] ---> array_push($ _ SESSION ['items'] ['p_fee'],'5.00')}
if $ seller_id(2002)== ['p_seller'] ---> array_push($ _ SESSION ['items']] ['p_fee'],'3.00')}
可以做到這一點嗎?
的$_SESSION['items']
最後可能包含如下內容:
Array
(
[0] => Array
(
[p_name] => Product A
[p_price] => 13.90
[p_seller] => 2001
[p_fee] => 5.00
)
[1] => Array
(
[p_name] => Product B
[p_price] => 2.00
[p_seller] => 2002
[p_fee] => 3.00
)
[2] => Array
(
[p_name] => Product C
[p_price] => 1.20
[p_seller] => 2002
[p_fee] => 3.00
)
[3] => Array
(
[p_name] => Product D
[p_price] => 50.00
[p_seller] => 2001
[p_fee] => 5.00
)
)
這似乎是合乎邏輯的我修改2個查詢我假設你正在運行從一個查詢生成該數據。那麼你不需要這樣的小提琴 – RiggsFolly