What Is a Clever Way to Count Number of Unique Items in Array (Php)

PHP - Count unique in an array of objects

https://www.php.net/manual/en/function.array-column.php :

Version    Description

7.0.0 Added the ability for the input parameter to be an array of objects.

Use array_column to generate new keys using the session column values. This effectively removes duplicate keys.

Code: (Demo)

$array = [
(object)['label' => 'Test 1', 'session' => 2],
(object)['label' => 'Test 2', 'session' => 2],
(object)['label' => 'Test 3', 'session' => 42],
(object)['label' => 'Test 4', 'session' => 9],
];
echo sizeof(array_column($array, null, 'session'));

Output:

3

Or in a loop:

foreach ($array as $obj) {
$result[$obj->session] = null;
}
echo sizeof($result);

Both techniques avoid the extra function call of array_unique and leverage the fact that arrays cannot store duplicate keys.

How to count unique value in array in php

As object can have same keys I will assume you have array of object for each restaurant object.

First I recommend get only the countries to separate array as:

foreach($objectArray as $restaurant)
$countries[] = $restaurant->location->country;

Now you can use array-count-values to get number of object according the countries:

print_r(array_count_values($countries));

How do I count occurrence of duplicate items in array

array_count_values, enjoy :-)

$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);

Result:

No. of NON Duplicate Items: 7
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)

Counting unique arrays inside the array in PHP?

Off the top of my head you could try:

$hashes = array();
$uniques = 0;
foreach($array as $slip) {
$hash = sha1(serialize($slip));
if(!in_array($hash, $hashes)) {
++$uniques;
$hashes[] = $hash;
}
}
var_dump($uniques); // prints total number of unique objects.

Edit:
@biakaveron's idea looks better though and could be adapted to:

$uniques = count(array_unique($array, SORT_REGULAR));
var_dump($uniques); // prints total number of unique objects.

How to count how many duplicates an array has in Php

Thanks for all your input. With that I came to the following solution which fits my demand the best:

<?php

function countValueInArray($value, $array) {
$count = 0;
for ($i=0; $i < count($array); $i++) {
if($value == $array[$i]) {
$count++;
}
}
return $count;
}

$numbers = array();

for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}

$duplicates = array();
for ($x=0; $x < count($numbers); $x++) {
$number = countValueInArray($numbers[$x], $numbers);

if ($number > 1) {
array_push($duplicates, $numbers[$x]);
}
}

$duplicatesList = array_values(array_unique($duplicates));

echo "number of duplicates: ".count($duplicatesList);
echo "<br>these are: <br>";

print_r($duplicatesList);

?>

Thanks a lot for your help!

how can i count distinct values associative array in php

The text you have there is a JSON. Just decode it an use the same method to count categories as in the function you previously provided. Check this out: http://www.php.net/manual/en/function.json-decode.php

Hope I could help, all the best...

LE:

$arr=json_decode($str);
foreach($arr['data'] as $value){
$categCount[$value['category']]++;
}

var_dump($categCount);// should give you the categories count



Related Topics



Leave a reply



Submit