如何计算多维数组中的所有值?
How to count all values in a multidimensional array?
我已经尝试过许多类似问题的解决方案,但它们似乎都为我提供了每个数组的计数。所以我有以下数组:
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 等行
我将如何计算所有重复项?
通过使用累加器数组并迭代所有元素,您可以很容易地做到这一点:
$result = [];
foreach ($data as $row) {
foreach($row as $value) {
$result[$value] = isset($result[$value]) ? $result[$value] + 1 : 1;
}
}
var_dump($result);
由于您的数组没有展平,除非您想调用合并函数,否则您将需要访问每个值并递增。
代码:(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)));
您可以对该结果使用 call_user_func_array
to merge all the individual arrays, and then array_count_values
:
$data = array
(array(1, 12, 2),
array(1, 13, 3),
array(1, 12, 2),
array(1),
array(1)
);
print_r(array_count_values(call_user_func_array('array_merge', $data)));
输出:
Array
(
[1] => 5
[12] => 2
[2] => 2
[13] => 1
[3] => 1
)
我已经尝试过许多类似问题的解决方案,但它们似乎都为我提供了每个数组的计数。所以我有以下数组:
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 等行
我将如何计算所有重复项?
通过使用累加器数组并迭代所有元素,您可以很容易地做到这一点:
$result = [];
foreach ($data as $row) {
foreach($row as $value) {
$result[$value] = isset($result[$value]) ? $result[$value] + 1 : 1;
}
}
var_dump($result);
由于您的数组没有展平,除非您想调用合并函数,否则您将需要访问每个值并递增。
代码:(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)));
您可以对该结果使用 call_user_func_array
to merge all the individual arrays, and then array_count_values
:
$data = array
(array(1, 12, 2),
array(1, 13, 3),
array(1, 12, 2),
array(1),
array(1)
);
print_r(array_count_values(call_user_func_array('array_merge', $data)));
输出:
Array
(
[1] => 5
[12] => 2
[2] => 2
[13] => 1
[3] => 1
)