Jump to content

counting unique value in an array


devan

Recommended Posts

Hi guys,

 

How would I write a small script in php to count the unique values in the below array :

 

the below code if part of cubecart 3.20 shopping cart, I need to count the unique number of  Seller_ID fields within this array in the basket

to be able to calculate shipping according to the number of sellers. How would I to this ? please any help is appreciated.

 

 

if(isset($_GET['act']) && $_GET['act']=="step4"){
		// set live vars for order inv and its the last step
		$view_cart->assign("QUAN_DISABLED","disabled");
		$view_cart->assign("TEXT_BOX_CLASS","textboxDisabled");
		$basket = $cart->setVar($productId,"productId","invArray",$i);
		$basket = $cart->setVar($product[0]['name'],"name","invArray",$i);
		$basket = $cart->setVar($product[0]['productCode'],"productCode","invArray",$i);
		$basket = $cart->setVar($plainOpts,"prodOptions","invArray",$i);
		$basket = $cart->setVar(sprintf("%.2f",$price*$quantity),"price","invArray",$i);
		$basket = $cart->setVar($quantity,"quantity","invArray",$i);
		$basket = $cart->setVar($product[0]['digital'],"digital","invArray",$i);
		$basket = $cart->setVar($product[0]['Prod_Seller'],"Prod_Seller","invArray",$i);
		$basket = $cart->setVar($product[0]['Seller_ID'],"Seller_ID","invArray",$i);

	}  else {
		$basket = $cart->unsetVar("invArray");
	}

Link to comment
https://forums.phpfreaks.com/topic/252323-counting-unique-value-in-an-array/
Share on other sites

Without knowing more about the application, I can only show you code that is similar to what you want to do.

 

$sellers_count = array();

foreach( $sellers_array as $seller )
{
     if( ! in_array( $seller, $sellers_count ) )
     {
           $sellers_count[] = $seller;
     }
}

$total_unique_sellers = count( $sellers_count );

 

If you simply replace $sellers_array with the actual array of sellers, you should be able to use $total_unique_sellers as your count of unique sellers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.