Php 如何计算数组中重复项的出现次数
我想计算数组中每个重复项的出现次数,并最终得到一个仅包含唯一/非重复项的数组,这些重复项具有各自的出现次数 这是我的密码;但我不知道哪里出了问题Php 如何计算数组中重复项的出现次数,php,arrays,count,Php,Arrays,Count,我想计算数组中每个重复项的出现次数,并最终得到一个仅包含唯一/非重复项的数组,这些重复项具有各自的出现次数 这是我的密码;但我不知道哪里出了问题 <?php $array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21); //$previous[value][Occurrence] for($arr = 0; $arr < count($array); $arr++){ $current = $array[$arr
<?php
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
//$previous[value][Occurrence]
for($arr = 0; $arr < count($array); $arr++){
$current = $array[$arr];
for($n = 0; $n < count($previous); $n++){
if($current != $previous[$n][0]){// 12 is not 43 -----> TRUE
if($current != $previous[count($previous)][0]){
$previous[$n++][0] = $current;
$previous[$n++][1] = $counter++;
}
}else{
$previous[$n][1] = $counter++;
unset($previous[count($previous)-1][0]);
unset($previous[count($previous)-1][1]);
}
}
}
//EXPECTED VALUES
echo 'No. of NON Duplicate Items: '.count($previous).'<br><br>';// 7
print_r($previous);// array( {12,1} , {21,2} , {43,6} , {66,1} , {56,1} , {78,2} , {100,1})
?>
,享受:-)
$array=数组(12,43,66,21,56,43,43,78,78100,43,43,43,21);
$VAL=数组\计数\值($array);
回显“非重复项目的数量:”。计数($VAL)。“
”;
打印(VAL);
结果:
非重复项目数量:7
排列
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)
实际上,我最近编写了一个函数,可以检查数组中的子字符串,在这种情况下,它会派上用场
function strInArray($haystack, $needle) {
$i = 0;
foreach ($haystack as $value) {
$result = stripos($value,$needle);
if ($result !== FALSE) return TRUE;
$i++;
}
return FALSE;
}
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
for ($i = 0; $i < count($array); $i++) {
if (strInArray($array,$array[$i])) {
unset($array[$i]);
}
}
var_dump($array);
函数strInArray($haystack,$needle){
$i=0;
foreach($haystack作为$value){
$result=stripos($value,$needle);
如果($result!==FALSE),则返回TRUE;
$i++;
}
返回FALSE;
}
$array=数组(12,43,66,21,56,43,43,78,78100,43,43,43,21);
对于($i=0;$i
PHP为您提供了一个神奇的函数,名为
使用部分代码,我们将修改循环,如下所示:
<?php
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$arr2 = array();
$counter = 0;
for($arr = 0; $arr < count($array); $arr++){
if (in_array($array[$arr], $arr2)) {
++$counter;
continue;
}
else{
$arr2[] = $array[$arr];
}
}
echo 'number of duplicates: '.$counter;
print_r($arr2);
?>
上述代码段将返回重复项的总数,即从样本数组中43重复5次,78重复1次,21重复1次,然后返回一个不重复的数组 如果您想尝试不使用
'array\u count\u value'
在这里你可以用一种聪明的方法
<?php
$input= array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$count_values = array();
foreach ($input as $a) {
@$count_values[$a]++;
}
echo 'Duplicates count: '.count($count_values);
print_r($count_values);
?>
我从谷歌来到这里,寻找一种方法来计算数组中重复项的出现次数。以下是简单的方法:
$colors = array("red", "green", "blue", "red", "yellow", "blue");
$unique_colors = array_unique($colors);
// $unique colors : array("red", "green", "blue", "yellow")
$duplicates = count($colors) - count($unique_colors);
// $duplicates = 6 - 4 = 2
if( $duplicates == 0 ){
echo "There are no duplicates";
}
echo "No. of Duplicates are :" . $duplicates;
// Output: No. of Duplicates are: 2
数组_unique()如何工作?
它包含所有重复项。
ex:
假设我们有一个数组,如下所示-
$cars=array([0]=>“兰博”、[1]=>“法拉利”、[2]=>“莲花”、[3]=>“法拉利”、[4]=>“布加迪”)代码>
当您执行以下操作时,$cars=array\u unique($cars)代码>
汽车将只有以下元素。
$cars=array([0]=>“兰博”、[1]=>“法拉利”、[2]=>“莲花”、[4]=>“布加迪”)代码>
欲了解更多信息:
希望它对那些从谷歌来这里寻找数组中重复值计数方法的人有所帮助。你也可以将它与文本项数组一起使用,你将正确地获得重复值的数量,但PHP显示了这一点
警告:数组\计数\值():只能对字符串和整数进行计数
价值观
在PHP中计算数组的重复元素,而不使用内置
作用
$arraychars=数组(“或”、“红色”、“黄色”、“绿色”、“红色”、“黄色”、“黄色”);
$arrCount=array();
对于($i=0;$i=1)
{
$arrCount[$key]++;
}否则{
$arrCount[$key]=1;
}
echo$arraychars[$i]。“
”;
}
回声“;
打印费用($arrCount);
如果您有一个多维数组,您可以在PHP 5.5+上使用它:
[
'keyA' => 4,
'keyB' => 2,
]
哪一个返回例如
$arrayVal = array(1,2,3,1,2,3,1,2,3,4,4,5,6,4,5,6,88);
$set_array = array();
foreach ($array as $value) {
$set_array[$value]++;
}
print_r($set_array);
您可以使用foreach循环来完成
Array( [1] => 3
[2] => 3
[3] => 3
[4] => 3
[5] => 2
[6] => 2
[88] => 1
)
输出:-
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
foreach($arr as $key=>$item){
if(array_count_values($arr)[$item] > 1){
echo "Found Matched value : ".$item." <br />";
}
}
此代码将返回相同数组中的重复值
$input = [1,2,1,3,2,4,10];
//if give string
//$input = "hello hello how are you how hello";
//$array = explode(' ',$input);
$count_val = [];
foreach($array as $val){
$count_val[$val]++;
}
print_r($count_val);
//output ( [1] => 2 [2] => 2 [3] => 1 [4] => 1 [10] => 1 )
$array=数组(12,43,66,21,56,43,43,78,78100,43,43,43,21);
foreach($arr as$key=>$item){
if(数组计数值($arr)[$item]>1){
echo“找到匹配的值:.$item.”
;
}
}
然后使用array\u键($array)获得所需的输出数组;PS所有人都需要处理变量Naming值得注意的是,这只适用于整数和字符串。如果你有一个数组或对象数组,你需要自己遍历数组,计算重复项。Hi Sam T的可能重复项。我喜欢你的方法,如何以智能的方式完成。你能给我解释一下@$count_值[$a]+];什么@代表什么以及它的作用,谢谢your@KrishnaJonnalagadda它只是抑制警告。
Array( [1] => 3
[2] => 3
[3] => 3
[4] => 3
[5] => 2
[6] => 2
[88] => 1
)
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
foreach($arr as $key=>$item){
if(array_count_values($arr)[$item] > 1){
echo "Found Matched value : ".$item." <br />";
}
}
$input = [1,2,1,3,2,4,10];
//if give string
//$input = "hello hello how are you how hello";
//$array = explode(' ',$input);
$count_val = [];
foreach($array as $val){
$count_val[$val]++;
}
print_r($count_val);
//output ( [1] => 2 [2] => 2 [3] => 1 [4] => 1 [10] => 1 )
$search_string = 4;
$original_array = [1,2,1,3,2,4,4,4,4,4,10];
$step1 = implode(",", $original_array); // convert original_array to string
$step2 = explode($search_string, $step1); // break step1 string into a new array using the search string as delimiter
$result = count($step2)-1; // count the number of elements in the resulting array, minus the first empty element
print_r($result); // result is 5