Viewed   84 times

I am having problem with updating an array element with in $_SESSION variable of PHP. This is the basic structure:

$product = array();
$product['id'] = $id;
$product['type'] = $type;
$product['quantity'] = $quantity;

And then by using array_push() function I insert that product in SESSION variable.

array_push($_SESSION['cart'], $product); 

Now this is the main part where i m facing problem:

foreach($_SESSION['cart'] as $product){

    if($id == $product['id']){
        $quantity = $product['quantity'];
        $quantity += 1;
        $product['quantity'] = $quantity;       
    }

}

I want to increment product quantity within $_SESSION['cart'] variable. How can I do that?

 Answers

3

Don't blindly stuff the product into your session. Use the product's ID as the key, then it's trivial to find/manipulate that item in the cart:

$_SESSION['cart'] = array();
$_SESSION['cart'][$id] = array('type' => 'foo', 'quantity' => 42);

$_SESSION['cart'][$id]['quantity']++; // another of this item to the cart
unset($_SESSION['cart'][$id]); //remove the item from the cart
Monday, August 15, 2022
 
zara
 
2
// first : get all data, if the data same / duplicate take only one data

$unique = array_unique($data, SORT_REGULAR);

// then, get the data which duplicate with

$diffCellUniq = array_diff_key($data, $unique);

// so print the result

print_r($diffCellUniq); exit;

Array
(
    [2] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

)
Tuesday, December 27, 2022
 
1

Don't use strcmp :)

function custom_sort($a, $b) {
    return $a['revenue_certificate'] - $b['revenue_certificate'];
}

usort($data_array, 'custom_sort');

custom_sort should return a negative, 0, positive value when $a < $b, $a == $b, $a < $b respectively (just as strcmp does BTW).

Tuesday, September 20, 2022
 
groomsy
 
3

Try this:

function getLastId(&$array){
    $tmp=end($array);
    return $tmp['id'];
}
Monday, September 19, 2022
 
jigfox
 
3

You can do it like this, do the calculation from the innermost of the array. Check the demo.

<?php
function f(&$array)
{
    foreach($array as $k => &$v)
    {
        if(is_array($v))
        {
            if(count($v) == count($v, 1))
            {
                unset($array[$k]);
                if($k == 'sum')
                {
                    $v =  array_sum($v);
                    $array[] = $v;

                }elseif($k == 'multiply'){
                    $v = array_product($v);
                    $array[] = $v;
                }else{

                    foreach($v as $vv)
                        $array[] = $vv;
                }
            }else
                f($v);
        }
    }
}

while(count($array) != count($array, 1))
{
    f($array);
}

print_r($array);

Noteļ¼š

traverse array from outer to inner
traverse array from inner to outer

Monday, November 14, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :