我已经尝试过许多类似问题的解决方案,但它们似乎都给了我每个数组的计数.所以我有以下数组:
Array
(
[1] => Array
(
[0] => 1
[1] => 12
[2] => 2
)
[2] => Array
(
[0] => 1
[1] => 13
[2] => 3
)
[3] => Array
(
[0] => 1
[1] => 12
[2] => 2
)
[4] => Array
(
[0] => 1
)
[5] => Array
(
[0] => 1
)
)
我试图计算所有数组的重复项.所以输出应该显示:
Five 1's
Two 12's
One 13
Two 2's
目前我正在尝试:
foreach($data as $key => $row) {
print_r(array_count_values($row));
}
其中输出每个单独阵列的计数
Array
(
[1] => 1
[12] => 1
[2] => 1
)
Array
(
[1] => 1
[13] => 1
[3] => 1
)
Array
(
[1] => 1
[12] => 1
[2] => 1
)
Array
(
[1] => 1
)
Array
(
[1] => 1
)
我也试过这个:
foreach ($data as $key => $row) {
$counts = array_count_values(array_column($data, $key));
var_dump($counts);
}
这似乎错过了很多信息,比如1的计数
array(2) {
[12]=>
int(2)
[13]=>
int(1)
}
array(2) {
[2]=>
int(2)
[3]=>
int(1)
}
array(0) {
}
array(0) {
}
array(0) {
}
注意,初始数组键并不总是顺序的,因为它代表行号.所以这个数组可能包含行1,2,5,6,7等.
我将如何计算所有重复项?
解决方法:
由于您的数组未展平,因此除非要调用合并函数,否则需要访问每个值并增加.
代码:(Demo)
$array = [
1 => [1, 12, 2],
2 => [1, 13, 3],
3 => [1, 12, 2],
4 => [1],
5 => [1]
];
// make the generated value available outside of function scope
// \-------------------------------v--------------------------/
array_walk_recursive($array, function($v)use(&$output) { // visit each leafnode
if (isset($output[$v])) { // check if the key has occurred before
++$output[$v]; // increment
} else {
$output[$v] = 1; // declare as 1 on first occurrence
}
});
var_export($output);
输出:
array (
1 => 5,
12 => 2,
2 => 2,
13 => 1,
3 => 1,
)
或者,非递归:
foreach ($array as $row) {
foreach ($row as $v) {
if (isset($output[$v])) { // check if the key has occurred before
++$output[$v]; // increment
} else {
$output[$v] = 1; // declare as 1 on first occurrence
}
}
}
或者,一个功能性的单线,以平坦然后计数:
var_export(array_count_values(array_reduce($array, 'array_merge', array())));
或者,带有splat运算符的功能性单行程平坦然后计数:
var_export(array_count_values(array_merge(...$array)));