php session array add an element if same ID

220 views Asked by At

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
        )

)
2

There are 2 answers

0
Blaatpraat On BEST ANSWER

Yes, you can.

You can do this by adding the value, while retrieving it from the database. For example:

while($res = $shipment->fetch_object()){
    $seller_id = $res->seller;
    $shipment_cost = $res->shipment_fee;

    foreach ($_SESSION['items'] as $key => $item) {
        if ($item['p_seller'] === $seller_id) {
            $_SESSION['items'][$key]['p_fee'] = $shipment_cost;
        }
    }
}

A better (faster) way is to create a mapping between the sellerId and costs, and afterwards put it in the session:

$costMapping = array();
while($res = $shipment->fetch_object()){
    $seller_id = $res->seller;
    $shipment_cost = $res->shipment_fee;

    $costMapping[$seller_id] = $shipment_cost;
}

foreach ($_SESSION['items'] as $key => $item) {
    if (isset($costMapping[$item['p_seller']])) {
        $_SESSION['items'][$key]['p_fee'] = $costMapping[$item['p_seller']];
    }
}

This way you only have 2 loops, whatever the amount of sellers is that you have.

0
webNeat On

Yes It's possible, you should just do two loops, first to collect fees and second to associate them with the items.

// Fill the fees first
$fees = [];
while($res = $shipment->fetch_object()){
    $seller_id = $res->seller;
    $shipment_cost = $res->shipment_fee;
    $fees[$seller_id] = $shipment_fee;
}

// Assign fees
foreach ($_SESSION['items'] as &$item) {
    $fee = $fees[$item['p_seller']] ?: 0; // <- fee will be 0 if not found !
    $item['p_fee'] = $fee;
}

Hope this helps :)