Re: [SOLVED] Merging Duplicate array items

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think I solved the issue i was having... I would be willing for any
optimization though.. .here is what I did
------------------------- code -------------

$ARY_cart[] = array(	'product_id' => '123',
		'sku' => 'DFBag-12',
		'quantity' => '2',
		'product_name' => 'Dog Food',
		'unit' => '12 lbs. Bag',
		'price' => '12.05'
		);
$ARY_cart[] = array(	'product_id' => '224',
		'sku' => 'cFBag-6',
		'quantity' => '1',
		'product_name' => 'Cat Food',
		'unit' => '8 lbs. Bag',
		'price' => '4.25'
		);
$ARY_cart[] = array(	'product_id' => '123',
		'sku' => 'DFBag-12',
		'quantity' => '24',
		'product_name' => 'Dog Food',
		'unit' => '12 lbs. Bag',
		'price' => '12.05'
		);
						
$cart_count = 0;
$cart_total = count($ARY_cart);
foreach ($ARY_cart as $key => $ary_value)
	{		
	do{
		if($ary_value['product_id'] == $ARY_cart[$cart_count]['product_id']
&& $key <> $cart_count)
		{
		$ARY_cart[$cart_count]['quantity'] =
$ARY_cart[$cart_count]['quantity'] + $ARY_cart[$key]['quantity'];
		unset($ARY_cart[$key]);
		}
		$cart_count++;
		} while ($cart_count == $cart_total);
		
		$cart_count = 0;
	}				


On 6/18/05, Joe Harman <cjharman@xxxxxxxxx> wrote:
> Hello, I've been trying to figure out on how I could evaluated this
> array and combine like items... here is the array.. it represents a
> shopping cart contents.. you'll notice that the first array is the
> same as the third... what I am trying to do is combine the quantity of
> the first array and the third... I am not asking from someone to
> program this for me... just for someone to give me a heads up on if
> there is a array function I could use. Thanks for any help!
> 
> ------------- array example --------------------
> 
> $ARY_cart[] = array(    'product_id' => '123',
>                                                                'sku' => 'DFBag-12',
>                                                                'quantity' => '2',
>                                                                'product_name' => 'Dog Food',
>                                                                'unit' => '12 lbs. Bag',
>                                                                'price' => '12.05'
>                                                                );
> $ARY_cart[] = array(    'product_id' => '224',
>                                                                'sku' => 'cFBag-6',
>                                                                'quantity' => '1',
>                                                                'product_name' => 'Cat Food',
>                                                                'unit' => '8 lbs. Bag',
>                                                                'price' => '4.25'
>                                                                );
> $ARY_cart[] = array(    'product_id' => '123',
>                                                                'sku' => 'DFBag-12',
>                                                                'quantity' => '24',
>                                                                'product_name' => 'Dog Food',
>                                                                'unit' => '12 lbs. Bag',
>                                                                'price' => '12.05'
>                                                                );
> 
> 
> --
> Joe Harman
> ---------
> Do not go where the path may lead, go instead where there is no path
> and leave a trail. - Ralph Waldo Emerson
> 


-- 
Joe Harman
---------
Do not go where the path may lead, go instead where there is no path
and leave a trail. - Ralph Waldo Emerson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux