php php数组中重复元素的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13413465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
count of duplicate elements in an array in php
提问by I'm nidhin
Hi,
How can we find the count of duplicate elements in a multidimensional array?
嗨,我们如何找到 a 中重复元素的数量multidimensional array?
I have an array like this
我有一个这样的数组
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 ?
如何找到每个元素的数量?
i.e, count of entries with id 192,202etc
即算有ID的条目192,202等
回答by Ja?ck
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.
你可以采用这个技巧;将数组的每个项目(它本身就是一个数组)映射到其各自的['lid']成员,然后用于array_count_value()为您进行计数。
array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);
Plus, it's a one-liner, thus adding to elite hacker status.
另外,它是单线的,因此增加了精英黑客的地位。
Update
更新
Since 5.5 you can shorten it to:
从 5.5 开始,您可以将其缩短为:
array_count_values(array_column($arr, 'lid'));
回答by Cedrun
foreach ($array as $value)
{
$numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value)
{
echo 'numbers of '.$key.' equal '.$value.'<br/>';
}
回答by shishir mishra
Following code will count duplicate element of an array.Please review it and try this code
以下代码将计算数组的重复元素。请查看并尝试此代码
$arrayChars=array("green","red","yellow","green","red","yellow","green");
$arrLength=count($arrayChars);
$elementCount=array();
for($i=0;$i<$arrLength-1;$i++)
{
$key=$arrayChars[$i];
if($elementCount[$key]>=1)
{
$elementCount[$key]++;
} else {
$elementCount[$key]=1;
}
}
echo "<pre>";
print_r($elementCount);
OUTPUT:
Array
(
[green] => 3
[red] => 2
[yellow] => 2
)
输出:
数组([绿色] => 3 [红色] => 2 [黄色] => 2)
You can also view similar questions with array handling on following link http://solvemyquest.com/count-duplicant-element-array-php-without-using-built-function/
您还可以在以下链接上查看与数组处理类似的问题 http://solvemyquest.com/count-duplicant-element-array-php-without-using-built-function/
回答by LeonardChallis
The following code will get the counts for all of them - anything > 1 at the end will be repeated.
以下代码将获得所有这些的计数 - 最后任何 > 1 的内容都将被重复。
<?php
$lidCount = array();
$lnameCount = array();
foreach ($yourArray as $arr) {
if (isset($lidCount[$arr['lid']])) {
$lidCount[$arr['lid']]++;
} else {
$lidCount[$arr['lid']] = 1;
}
if (isset($lnameCount [$arr['lname']])) {
$lnameCount [$arr['lname']]++;
} else {
$lnameCount [$arr['lname']] = 1;
}
}
回答by Arun
$array = array('192', '202', '192', '202');
print_r(array_count_values($array));
回答by rOcKiNg RhO
$orders = array(
array(
'lid' => '',
'lname' => '',
))....
$foundIds = array();
foreach ( $orders as $index => $order )
{
if ( isset( $foundIds[$order['lid']] ) )
{
$orders[$index]['is_dupe'] = true;
$orders[$foundIds[$order['lid']]]['is_dupe'] = true;
} else {
$orders[$index]['is_dupe'] = false;
}
$foundIds[$order['lid']] = $index;
}
回答by Lao
Try this code :
试试这个代码:
$array_count = array();
foreach ($array as $arr) :
if (in_array($arr, $array_count)) {
foreach ($array_count as $key => $count) :
if ($key == $arr) {
$array_count[$key]++;
break;
}
endforeach;
} else {
$array_count[$arr] = 1;
}
endforeach;
回答by Anand Pal
Check with in_array()function.
检查in_array()功能。

