P粉3230507802023-09-06 09:57:01
Using array_reduce
In a special and useful way, we can group items by name. Then group by value and count. The idea is to pass an array with accumulated values as keys.
$g = array($a, $b, $c, $d, $e, $f, $h);
$result = array_reduce($g, function ($carry, $item) {
$key = $item[0];
$value = $item[1];
if (!isset($carry[$key])) {
$carry[$key] = [];
}
if (!isset($carry[$key][(string) $value])) {
$carry[$key][(string) $value] = 0;
}
$carry[$key][(string) $value]++;
return $carry;
}, []);
print_r($result);
Output:
Array ( [FA] => Array ( [12.9] => 3 [12.4] => 1 ) [FB] => Array ( [12.2] => 1 [12.9] => 1 ) [FC] => Array ( [12.3] => 1 ) )