This question already has an answer here:
这个问题在这里已有答案:
- Count duplicate values in multidimensional array 2 answers
- PHP: Check for duplicate values in a multidimensional array 5 answers
计算多维数组2答案中的重复值
PHP:检查多维数组中的重复值5个答案
I have this array:
我有这个数组:
Array ( [55118] => Array ( [id] => 55118
[usr_name] => Name 1
[usr_employment] => Fulltime
)
[55179] => Array ( [id] => 55179
[usr_name] => Name 2
[usr_employment] => Fulltime
)
[55549] => Array ( [id] => 55549
[usr_name] => Name 1
[usr_employment] => Fulltime
)
)
Now 'd like to count how many times "Name 1" exist in my array.
My problem is that it's a 2D array.
现在我想计算我的数组中存在“名称1”的次数。我的问题是它是一个二维数组。
So i'd like to print:
所以我想打印:
Name 1, 2 times
Name 2, 1 time
名称1,2次名称2次,1次
I can't find a correct answer to this question before.
我以前找不到这个问题的正确答案。
1 个解决方案
#1
3
You can use array_count_values and array_column.
您可以使用array_count_values和array_column。
$counts = array_count_values(array_column($arr, "usr_name"));
This should give you an associative array with key being the name and the value being the count of that name.
这应该为您提供一个关联数组,其中键是名称,值是该名称的计数。
#1
3
You can use array_count_values and array_column.
您可以使用array_count_values和array_column。
$counts = array_count_values(array_column($arr, "usr_name"));
This should give you an associative array with key being the name and the value being the count of that name.
这应该为您提供一个关联数组,其中键是名称,值是该名称的计数。