重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
简单的循环即可,选出重复(出现次数大于等于2)的元素并统计每个重复元素出现的次数:
网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为成都高空作业车租赁等企业提供专业服务。
php中的 array_count_values() 函数可以实现
array_count_values() 函数用于统计数组中所有值出现的次数。
本函数返回一个数组,其元素的键名是原数组的值,键值是该值在原数组中出现的次数。
array_count_values(array)
例如:
?php
$a=array("Cat","Dog","Horse","Dog");
print_r(array_count_values($a));
?
输出:
Array ( [Cat] = 1 [Dog] = 2 [Horse] = 1 )
扩展资料:
php统计数组元素个数
count():对数组中的元素个数进行统计;
sizeof():和count()具有同样的用途,这两个函数都可以返回数组元素个数,得到一个常规标量变量中的元素个数。
如果传递给这个函数的数组是一个空数组,或者是一个没有经过设定的变量,返回的数组元素个数就是0;
array_count_value():统计每个特定的值在数组$array中出现过的次数;
如:
$array=array(4,5,1,2,3,1,2,1);
$ac=array_count_value($array);
将创建一个名为$ac数组,该数组包括:
关键字 值
4 1
5 1
1 3
2 2
3 1
本文实例讲述了PHP获取数组中重复最多的元素的实现方法。分享给大家供大家参考。具体方法如下:
复制代码
代码如下:?php
/**
*
*
Created
on
2014-4-1
*
@param
array
$array
*
@param
int
[optional]
$length
*
@return
array
*/
function
mostRepeatedValues($array,$length=0){
if(emptyempty($array)
or
!is_array($array)){
return
false;
}
//1.
计算数组的重复值
$array
=
array_count_values($array);
//2.
根据重复值
倒排序
arsort($array);
if($length0){
//3.
返回前
$length
重复值
$array
=
array_slice($array,
0,
$length,
true);
}
return
$array;
}
$array
=
array(1,
1,
1,
54,
3,4,
3,4,
3,
14,
3,4,
3,7,8,9,12,45,66,5,7,8,9,2,45);
$counts=mostRepeatedValues($array,5);
print_r($counts);
/*输出结果为:
Array
(
[3]
=
5
[4]
=
3
[1]
=
3
[9]
=
2
[45]
=
2
)
*/
?
希望本文所述对大家的PHP程序设计有所帮助。
这个问题有2层意思:
考虑数组为$arr = ("bob","bob","jose","rose","kid","rose","obo")
1,找出特定某个值重复了多少次,例如,找出数组中bob出现了多少次
$count = 0;
foreach($arr as $v)
{ if("bob" == $v )
$count ++;
}
这里求出来的$count 就是bob出现了多少次
2,找出重复过的值,而不是针对某个值。应该二重循环
$count = array(); //数组记录重复次数
$temp_count = 0;
foreach ( $arr as $ out )
foreach ($arr as $ in )
{
$temp_count = 0;
if($in == $out)
{
$temp_count ++;
if($temp_count 1)
{
$count[$in] = $temp_count;
}
}
}
这里的话,$count数组里面就是存放了重复过的键值和次数(大于1)
$arr=array(1,2,3,4,1,2);
foreach($arr as $k=$v){
$arrn[$v][]=$k;
}
foreach($arrn as $k=$v){
if(count($v)1){
echo $k.'重复下标为';
foreach($v as $vv){
echo $vv.'--';
}
}
}
随手打得,楼主看看可行吗
$arr = array(
array(
array(
"stocknotifyId" = 1,
"sku" = '13809_BL_37',
"email" = 'huang@aaa.com'
),
array(
"stocknotifyId" = 3,
"sku" = '13809_BL_37',
"email" = 'hrheve@dfwef.com'
),
array(
"stocknotifyId" = 5,
"sku" = '13809_BL_37',
"email" = 'dfsfs@dfsf.com'
)
),
array(
array(
"stocknotifyId" = 2,
"sku" = '13809_B_39',
"email" = 'huang@aaa.com'
)
),
array(
array(
"stocknotifyId" = 4,
"sku" = '13809_BL_39',
"email" ='hrheve@dfwef.com'
)
)
);
$re = array();
foreach($arr as $a) {
foreach ($a as $v) {
$tmp_v = $v;
unset($tmp_v['email']);
if(isset($re[$v['email']])) {
$re[$v['email']][] = $tmp_v;
}else{
$re[$v['email']] = array($tmp_v);
}
}
}
print_r($re);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
!-- output --
Array
(
[huang@aaa.com] = Array
(
[0] = Array
(
[stocknotifyId] = 1
[sku] = 13809_BL_37
)
[1] = Array
(
[stocknotifyId] = 2
[sku] = 13809_B_39
)
)
[hrheve@dfwef.com] = Array
(
[0] = Array
(
[stocknotifyId] = 3
[sku] = 13809_BL_37
)
[1] = Array
(
[stocknotifyId] = 4
[sku] = 13809_BL_39
)
)
[dfsfs@dfsf.com] = Array
(
[0] = Array
(
[stocknotifyId] = 5
[sku] = 13809_BL_37
)
)
)
?php
$arr1 = array("1","2","3","4","5","6","7","8","9","10");
$arr2 = array("11","12","13","14","15","16","17","18","19","20");
$a = 2;
$b = 6;
$c = 9;
$d = 11;
$e = 15;
$f = 20;
$x = $y = 0;
foreach(array($a, $b, $c, $d, $e, $f) as $test){
in_array($test, $arr1) $x++;
in_array($test, $arr2) $y++;
}
echo " x : ", $x, " y : ", $y, "\n";