有数组:
$data = [
array(
[0] => "Автобусные",
[1] => "Пешеходные"
),
array(
[0] => "Исторические",
[1] => "Пешеходные",
[2] => "Водные"
),
array(
[0] => "Пешеходные",
[1] => "Тематические",
[2] => "Исторические"
)
];
很长的路要走:
$result = [];
foreach ($data as $row) {
foreach ($row as $val) {
if(!empty($val)) array_push($result, $val);
}
}
$result = array_count_values($result);
结果:
Array(
[Автобусные] => 1
[Пешеходные] => 3
[Исторические] => 2
[Водные] => 1
[Тематические] => 1
)
有没有办法更快地重新计算数组中的值?
最初我们有以下数组:
方法
RecursiveIteratorIterator
:或使用的变体
call_user_func_array
:或使用的变体
array_reduce
: