I have a session string containing shopping cart product ids and quantities. For example, the cart session var looks like this: 11 3|12 1| 18 1| 18 1 |.
I want to combine duplicate items with the actual total desired so that in the cart view area there is only one line item per product.
Frist I am trimming the last char which is the "|" or I get an empty array element. I am then exploding that session string by | and then for each item that generates I am exploding (ex. 11 3) by " ". I am then creating 4 arrays:
for ($i = 0; $i < count($cart); $i++) {
echo "<BR>Cart Items" . $cart[$i]; $iq = explode (" ", $cart[$i]);
//keep safe arrays
$itemArray[$i] = $iq[0];
$qtyArray[$i] = $iq[1];
//arrays to play
$itemArray2[$i] = $iq[0];
$qtyArray2[$i] = $iq[1];
}
I am then creating a new array with the duplicate indexes in itemArray2. I am creating yet another array for the keys of this new array.
$checkKeysUniqueComparison = create_function('$value','if ($value > 1) return true;');
$result = array_keys (array_filter (array_count_values($itemArray2), $checkKeysUniqueComparison));
$keys = array_keys($result);
Then based on the count of the array $results I am creating an array of index...Anyways this goes on for a while and it fail miserably.
I need to be able to combine the duplicate items and total up the real quantities and then write the cart string back to the session variable in this format 11 3|12 1| 18 1| 18 1 |.
It can't be as hard as I am trying to make it. If anyone could shed some light on this, I would be totally grateful. Thanks for you consideration. I really appreciate it.