How to counting varians data from array?


I have an array like this

$arr = array(1,2,1,3,2,4);

How to count total of variant data from that array ?

example :

$arr = array(1,2,1,3,2,4); <blockquote>

total = 4



You can flip the array, and check its length:

echo count(array_flip($arr));

This works because an array index must be unique, so you end up with one element for every unique item in the original array:

<a href="http://php.net/manual/en/function.array-flip.php" rel="nofollow">http://php.net/manual/en/function.array-flip.php</a>


If a value has several occurrences, the latest key will be used as its value, and all others will be lost.


This is (was?) somewhat faster than array_unique, but unless you are calling this a LOT, array_unique is a lot more descriptive, so probably the better option


If you are trying to count the unique values in an array, this is very straightforward and rather obvious:

echo count(array_unique($arr));


  • PHP Replace multidimensional array keys
  • Fastest way to combine thousands of arrays uniquely in PHP
  • Reverse an array upside down?
  • multidimensional array
  • Slowly changing dimension - What is Pure type 6 implementation
  • SQL append distinct values from two columns and two tables
  • Hibernate: Inheritance and relationship mapping + generics
  • Should I use composite primary keys in Grails?
  • Who propagate bugfixes across branches (corporate development)?
  • xcode don't localize specific strings
  • Using Sax parsing to edit and write XML in VB6
  • Stop Bash Script if Hive Fails
  • C: Incompatible pointer type initializing
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Django: Count of Group Elements
  • ilmerge with a PFX file
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • ILMerge & Keep Assembly Name
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Weird JavaScript statement, what does it mean?
  • Large data - storage and query
  • Rearranging Cells in UITableView Bug & Saving Changes
  • WOWZA + RTMP + HTML5 Playback?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • Django query for large number of relationships
  • Running Map reduces the dimensions of the matrices
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?