我有这个数组:
Array ( [0] => Array ( [Num] => 2 ) [1] => Array ( [Num] => 3 ) [2] => Array ( [Num] => 13 ) [3] => Array ( [Num] => 2 ) [4] => Array ( [Num] => 3 ) [5] => Array ( [Num] => 3 ) [6] => Array ( [Num] => 1 ) [7] => Array ( [Num] => 44 ) )
我想得到重复值的数量,例如该数组有5个值重复。 这个问题的简单方法??帮助我!
php代码:
$count = 0;
$temp = array();
for($i=0;$i<=sizeof($Num);$i++){
for($j=$i+1;$j<=sizeof($Num);$j++){
if($Num[$i]==$Num[$j]){
$count++;
$temp[]=$Num[$j];
}
}
}
var_dump($temp);
echo $count;
答案 0 :(得分:1)
从PHP 5.5开始(因为array_column *)你可以使用
Start proc com.**.** for activity com.**.**/com.**.**: pid=11091 uid=10146 gids={50146, 3003, 1028, 1015} by log Tag ActivityManager.
即。
- 获取所有元素&#39; Num&#39;在$ num中作为&#34;持平,1-d&#34;阵列。
- 计算每个值的出现次数
- 只在数组中保持count()&gt; 1
-
*在用户提供的array_column备注中有php&lt; 5.5
的填充内容
而且由于$ num中每个数组只有一个元素,你也可以使用array_map / array_shift
编辑:没有array_column的更详细的版本
<?php
$num = array (
array ( 'Num' => 2 ), array ( 'Num' => 3 ), array ( 'Num' => 13 ), array ( 'Num' => 2 ),
array ( 'Num' => 3 ), array ( 'Num' => 3 ), array ( 'Num' => 1 ), array ( 'Num' => 44 )
);
$x = array_filter(
array_count_values(
array_column($num, 'Num')
),
function($e) { return $e>1; }
);
var_export($x);
打印<?php
$num = array (
array ( 'Num' => 2 ), array ( 'Num' => 3 ), array ( 'Num' => 13 ), array ( 'Num' => 2 ),
array ( 'Num' => 3 ), array ( 'Num' => 3 ), array ( 'Num' => 1 ), array ( 'Num' => 44 )
);
// first remove the superfluous second dimension
$num = array_map(function($e) { return $e['Num']; }, $num); // -> $num = [ 2,3,13,2,3,3,1,44]
$num = array_count_values($num); // -> $num = [2=>2, 3=>3, 13=>1, 1=>1, 44=>1]
$num = array_filter($num, function($e) { return $e>1; }); // -> $num = [2=>2, 3=>3]
printf('There are %d duplicate values. A total of %d elements are doublets',
count($num), array_sum($num)
);
答案 1 :(得分:1)
解决它没有花哨的东西,但它应该工作:
function carousel() {
var timer, count = 0;
var loop = function loop(count) {
if (count === imgURLS.length) count = 0;
image.src = imgURLS[count];
timer = setTimeout(loop, 2000, ++count);
}
loop(count);
}
carousel();
答案 2 :(得分:0)
for PHP 5&gt; = 5.5.0,PHP 7
$Num = array (
"0" => array ( 'Num' => 2 ),
"1" => array ( 'Num' => 3 ),
"2" => array ( 'Num' => 13),
"3" => array ( 'Num' => 2 ),
"4" => array ( 'Num' => 3 ),
"5" => array ( 'Num' => 3 ),
"6" => array ( 'Num' => 1 ),
"7" => array ( 'Num' => 44 ) );
$subArr = array_column($Num, 'Num');
$countArr = array_count_values($subArr);//array with all count
$filteredArr = array_filter($countArr, function ($val) {return $val > 1;});//filter that is having duplicates
echo "Count of Duplicate : ".array_sum($filteredArr);
否则
$Num = array (
"0" => array ( 'Num' => 2 ),
"1" => array ( 'Num' => 3 ),
"2" => array ( 'Num' => 13),
"3" => array ( 'Num' => 2 ),
"4" => array ( 'Num' => 3 ),
"5" => array ( 'Num' => 3 ),
"6" => array ( 'Num' => 1 ),
"7" => array ( 'Num' => 44 ) );
$countArr = array();
foreach ($Num AS $eachArr) {
$countArr[$eachArr['Num']] += 1;
}
$filteredArr = array_filter($countArr, function ($val) {return $val > 1;});//filter that is having duplicates
echo "Count of Duplicate : ".array_sum($filteredArr);