更新多维购物车阵列中的值

I am writing a custom shopping cart in PHP, I add product info to the cart session like this:

$product_array=array($title,$price,$qty);
if(!isset($_SESSION['cart']))
$_SESSION['cart']=array();

if(!in_array($product_array,$_SESSION['cart']))
$_SESSION['cart'][]=$product_array;
else
{
// update price and quantity here
}

My challenge: I want to update the price and quantity($qty) of the product if it already exists in $_SESSION['cart'] array rather than adding it. Something like this

price = price + $price,
qty = qty + $qty

This example is similar to your example. you can add code from foreach loop in else condition. I am considering product_id instead of $title variable.

 $_SESSION['cart'][] = [ 'product_id'=> 12, 'price' => 100 , 'quantity' => 2 ];
 $_SESSION['cart'][] = [ 'product_id'=> 11, 'price' => 200, 'quantity' => 3 ];

    $product_array = ['product_id' => 11, 'price'=> 200, 'quantity' => 4 ];
    foreach( $_SESSION['cart'] as $key => $value ) {
      if( $value['product_id']  == $product_array['product_id']) {
         $_SESSION['cart'][$key]['quantity'] = $value['quantity'] + $product_array['quantity'];
           $_SESSION['cart'][$key]['price'] = $value['price'] + $product_array['price'];
        }
    }
print_r($_SESSION);

Output before updating product :

Array
(
    [cart] => Array
        (
            [0] => Array
                (
                    [product_id] => 12
                    [price] => 100
                    [quantity] => 2
                )
            [1] => Array
                (
                    [product_id] => 11
                    [price] => 200
                    [quantity] => 3
                )
        )
)

Output after adding new product in session.

Array
(
    [cart] => Array
        (
            [0] => Array
                (
                    [product_id] => 12
                    [price] => 100
                    [quantity] => 2
                )
            [1] => Array
                (
                    [product_id] => 11
                    [price] => 400
                    [quantity] => 7
                )
        )
)