[英]Count similar values in php array of array and add unique in a new array
I've got problems with some algo on a php array.我在 php 阵列上遇到了一些算法问题。 Here is a kind of example of what I get after a grep on several logs files and store it in an array.
这是我在几个日志文件上的 grep 之后得到的一种示例,并将其存储在一个数组中。 It can be longer, each element is the result of a grep depending of the number of logs file in folder).
它可以更长,每个元素都是 grep 的结果,具体取决于文件夹中日志文件的数量)。
(
[0] => Array
(
[0] => : [POST] /api_v2/resources/new_service_indexes
[1] => : [POST] /api_v2/resources/new_service_tree_categories
[2] => : [POST] /api_v2/users/add_device
[3] => : [POST] /api_v2/users/check_email
)
[1] => Array
(
[0] => : [GET] /api_v2/resources/categories
[1] => : [POST] /api_v2/resources/new_service_indexes
[2] => : [POST] /api_v2/resources/new_service_tree_categories
[3] => : [POST] /api_v2/users/add_device
[4] => : [POST] /api_v2/users/check_email
[5] => : [POST] /api_v2/users/facebook_login
[6] => : [POST] /api_v2/users/login
[7] => : [POST] /api_v2/users/register
)
[2] => Array
(
[0] => : [POST] /api_v2/resources/new_service_indexes
[1] => : [POST] /api_v2/resources/new_service_tree_categories
[2] => : [POST] /api_v2/users/add_device
[3] => : [POST] /api_v2/users/facebook_login
[4] => : [POST] /api_v2/users/login
)
)
I want to count all key occurrences before deleting them and get a new array where there is unique key and the count.我想在删除它们之前计算所有键的出现次数,并获得一个新数组,其中有唯一键和计数。 Example:
例子:
(
[name] => : [GET] /api_v2/resources/categories
[count] => : 2
[name] => : [POST] /api_v2/resources/new_service_indexes
[count] => : 3
[name] => : [POST] /api_v2/resources/new_service_tree_categories
[count] => : 5
[name] => : [POST] /api_v2/users/add_device
[count] => : 2
[name] => : [POST] /api_v2/users/check_email
[count] => : 2
[name] => : [POST] /api_v2/users/facebook_login
[count] => : 6
[name] => : [POST] /api_v2/users/login
[count] => : 2
[name] => : [POST] /api_v2/users/register
[count] => : 2
)
You can flatten the arrays and count the values:您可以展平 arrays 并计算值:
$result = array_count_values(array_merge(...$array));
Should yield something like:应该产生类似的东西:
Array (
[ : [GET] /api_v2/resources/categories] => 2
[ : [POST] /api_v2/resources/new_service_indexes] => 3
)
The result array that you show isn't possible unless it's also two-dimensions.您显示的结果数组是不可能的,除非它也是二维的。 To do that:
要做到这一点:
foreach($result as $key => $val) {
$new_result[] = ['name' => $key, 'count' => $val];
}
But it would probably be easier just to merge the arrays while executing the grep
, but you don't show that code.但是在执行
grep
时合并 arrays 可能会更容易,但是您没有显示该代码。 You could ask another question with that code to get a better array format or this end result.您可以使用该代码提出另一个问题,以获得更好的数组格式或最终结果。
You can use array_count_values
with array_merge
and ...
splat operator to get the counts and then use array_walk
to build the desired array您可以将
array_count_values
与array_merge
和...
splat 运算符一起使用来获取计数,然后使用array_walk
构建所需的数组
$new = array_count_values(array_merge(...$a));
array_walk($new, function($v, $k) use (&$res){
$res[] = ['name' => $k, 'count' => $v];
});
Example: https://3v4l.org/1UqA8示例: https://3v4l.org/1UqA8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.