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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.