I got below session array to store cart items:
print_r($_SESSION['items']):
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
)
)
I have a loop to call additional data from DB:
while($res = $shipment->fetch_object()){
$seller_id = $res->seller;
$shipment_cost = $res->shipment_fee;
//array_push($_SESSION['items']['p_fee'], $shipment_cost);
}
let says, in a loop I get below data:
$seller_id = 2001; $shipment_cost = 5.00
$seller_id = 2002; $shipment_cost = 3.00
I want to assign and add an extra element with name ['p_fee']
to its respective group ['p_seller']
if $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') }
is that possible to do that?
The $_SESSION['items']
may finally contain elements like:
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
)
)
Yes, you can.
You can do this by adding the value, while retrieving it from the database. For example:
A better (faster) way is to create a mapping between the sellerId and costs, and afterwards put it in the session:
This way you only have 2 loops, whatever the amount of sellers is that you have.