php会话数组添加一个元素,如果相同的ID

时间:2017-01-06 09:41:11

标签: php array-push

我得到了以下会话数组来存储购物车项目:

的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')}

     

如果$ 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 个答案:

答案 0 :(得分:4)

是的,你可以。

您可以通过添加值来执行此操作,同时从数据库中检索它。例如:

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;
        }
    }
}

更好(更快)的方法是在sellerId和cost之间创建一个映射,然后将其放入会话中:

$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']];
    }
}

这样你只有2个循环,无论你有多少卖家。

答案 1 :(得分:2)

是的,你应该做两个循环,先收取费用,然后再将它们与项目联系起来。

// 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;
}

希望这会有所帮助:)