在PHP数组中重复元素的数量 [英] count of duplicate elements in an array in php
本文介绍了在PHP数组中重复元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到multidimensional array
中重复元素的数量?
Hi,
How can we find the count of duplicate elements in a multidimensional array
?
我有一个像这样的数组
Array
(
[0] => Array
(
[lid] => 192
[lname] => sdsss
)
[1] => Array
(
[lid] => 202
[lname] => testing
)
[2] => Array
(
[lid] => 192
[lname] => sdsss
)
[3] => Array
(
[lid] => 202
[lname] => testing
)
)
如何查找每个元素的数量?
How to find the count of each elements ?
即ID为192
,202
等的条目数
推荐答案
您可以采用此技巧;将数组的每个项目(本身就是数组)映射到其各自的['lid']
成员,然后使用array_count_value()
为您进行计数.
You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid']
member and then use array_count_value()
to do the counting for you.
array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);
另外,它是单线的,因此增加了精英黑客的地位.
Plus, it's a one-liner, thus adding to elite hacker status.
从5.5开始,您可以将其缩短为:
Since 5.5 you can shorten it to:
array_count_values(array_column($arr, 'lid'));
这篇关于在PHP数组中重复元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文