- array_sum: documentation ( source)
- array_count_values: documentation ( source)
- array_reduce: documentation ( source)
<?php
function countUnique($a)
{
$acv=array_count_values($a);
$n=0;
foreach ($acv as $f) if ($f==1) $n++;
return $n;
}
function countUniqueTML($a)
{
return array_reduce(
array_count_values($a),
function($c, $i) { return ($i === 1) ? $c + 1 : $c; },
0);
}
function countUniqueGG($a)
{
$cardinal=[];
foreach($a as $v)
if (isset($cardinal[$v])) $cardinal[$v] = 0; else $cardinal[$v] = 1;
return array_sum($cardinal);
}
$a=[1,1,2,2,2,5,6];
echo countUniqueTML($a);
echo "\n";
echo countUnique($a);
echo "\n";
echo countUnique($a);