原数组
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] =>
)
[1] => Array
(
[0] => 2
[1] =>
)
[2] => Array
(
[0] => 2
[1] =>
)
[3] => Array
(
[0] => 2
[1] => 4
[2] =>
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] =>
)
[5] => Array
(
[0] => 2
[1] => 3
[2] =>
)
)
处理后的数组
Array
(
[0] => Array
(
[counts] => 2
[name] => 1
)
[1] => Array
(
[counts] => 6
[name] => 2
)
[2] => Array
(
[counts] => 2
[name] => 3
)
[3] => Array
(
[counts] => 1
[name] => 4
)
)
1
sadfQED2 2023-01-28 09:47:52 +08:00 via Android
写个递归函数处理?
|
3
admpubcom 2023-01-28 10:05:32 +08:00 via iPhone 1
使用一个临时的中间数组来统计,用要统计的值来作为数组的 key ,统计数作为数组的值
|
4
zakokun 2023-01-28 10:09:17 +08:00 1
循环第一个数组,放到中间数组,中间数组的 key 是值,value 是出现次数。 再循环这个中间数组,生成最终结果
|
5
shengtao 2023-01-28 10:19:43 +08:00 1
```php
$arr = []; foreach ($a as $v) { $arr = array_merge($arr, $v); } // 如果需要过滤 null 值的话 $arr = array_filter($arr, function ($item) { return !is_null($item); }); $c = array_count_values($arr); $result = []; foreach ($c as $key => $value) { $result[] = [ 'name' => $key, 'counts' => $value, ]; } var_dump($result); ``` |
6
phpdever 2023-01-28 10:37:16 +08:00 1
|
7
mumu 2023-01-29 01:46:01 +08:00 1
先合并数组,array_unique 去重,然后,substr_count
|
8
mumu 2023-01-29 01:47:41 +08:00
忘记说了,去重之前,join 一下,生成 string ,最后在 substr_count 获取 count
|