Forum Moderators: coopster
$var['item']['quantity'] = 10;
Also, if I have duplicates like these..
$var['item'] = 'foo';
$var['item']['quantity'] = 5;
$var['item'] = 'foo';
$var['item']['quantity'] = 11;
$var['item'] = 'foo';
$var['item']['quantity'] = 16;
function sortCart() {
if (isset($_SESSION['scart'])) {
$cart = $_SESSION['scart'];
return $cart;
} else {
$cart = $_SESSION['cart'];
$dupes = array_count_values($cart);
$keys = array_keys($dupes);
$arr = array();
$counter = count($dupes);
for($i = 0; $i < $counter; $i++) {
$arr[] = array('item' => $keys[$i], 'quantity' => $dupes[$i + 1]);
}
return $arr;
}
Any help would be appreciated! Thank You!
this case shouldn't happen
$var['item'] = 'foo';
$var['item']['quantity'] = 5;
$var['item'] = 'foo';
$var['item']['quantity'] = 11;
it should be handled in the add to cart portion of your script. Search for an existing item of the same name and add the quantity to what's already there
For this you can use in_array [php.net] to check to see if the value exists already. If it does, then add the new quantity to the one already in the array.
$value = $var['item']['quantity'];
$newvalue = $value + $quantity;
$var['item']['quantity'] = $newvalue;
How can I subtract values?
Also,
this case shouldn't happen$var['item'] = 'foo';
$var['item']['quantity'] = 5;
$var['item'] = 'foo';
$var['item']['quantity'] = 11;
happens because I been using array_count_values and it creates multiple keys...
Are you sure the multiple keys are coming from this function? It shouldn't...