What is a clever way to count number of unique items in array (PHP)? [duplicate]

If you want to get the total count of unique values in a specified column within a given array, as a simple integer (rather than another array) try something simple like this:

$uniqueCount = count(array_unique(array_column($data, 'column_name'))); 

// (where $data is your original array, and column_name is the column you want to cycle through to find the total unique values in whole array.)  

var_dump(array_count_values(array("bye", "bye", "bye", "hello", "hello")));

Leave a Comment