Php 如何计算数组值并仅保留相关的值?
我有一个包含多个整数的数组,我只对重复一定次数的整数感兴趣。例如:Php 如何计算数组值并仅保留相关的值?,php,arrays,Php,Arrays,我有一个包含多个整数的数组,我只对重复一定次数的整数感兴趣。例如: $items = (0, 0, 0, 1, 1, 2, 3, 3, 3) 我想知道哪些项被精确地重复了$number(在本例中$number=3)次(在本例中新数组$items=(0,3)) 如果没有一个数组项被重复$number次,我需要var$none=1 我知道函数数组的计数值,但不知道如何在我的情况下实现它…$repeated\u items将是一个仅包含所需项的数组 $limit = 3; //your limit
$items = (0, 0, 0, 1, 1, 2, 3, 3, 3)
我想知道哪些项被精确地重复了$number
(在本例中$number=3
)次(在本例中新数组$items=(0,3)
)
如果没有一个数组项被重复$number
次,我需要var$none=1
我知道函数数组的计数值,但不知道如何在我的情况下实现它…$repeated\u items将是一个仅包含所需项的数组
$limit = 3; //your limit for repetition
$catch = array();
foreach ($items as $item){
if(array_key_exists($item, $catch)){
$catch[$item]++;
} else {
$catch[$item] = 1;
}
}
$repeated_items = array();
foreach ($catch as $k=>$caught){
if($caught>=$limit){
$repeated_items[]=$k;
}
}
一些让您开始的伪代码:
Sort your array in order to get similar items together
Foreach item
if current item == previous item then
repeat count ++
else
if repeat count > limit then
add current item to new array
- 使用
获得每个数字出现频率的配对array\u count\u value
- 通过
回调对其进行过滤,该回调将丢弃除计数为array\u filter
$number
- 获取结果数组的键(实际计数值)
- 结果数组为空或包含出现次数
的值$number
有点晚了,但是:
<?php
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$temp = array_unique($items);
$result = array();
$none = 1;
$number = 3;
foreach($temp as $tmp)
{
if(count(array_keys($items, $tmp)) == $number)
{
array_push($result,$tmp);
$none = 0;
}
}
print_r($result);
?>
我知道有很多解决方案,但我想再添加一个。;-)
一种方法是创建一种哈希表并循环数组中的每个项
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$number = 3;
$none = 1;
foreach ($items as $value) {
if ($hash[$value] >= $number) {
# This $value has occured as least $number times. Lets save it.
$filtered_items[] = $value;
# We have at least one item in the $items array >= $number times
# so set $none to 0
$none = 0;
# No need to keep adding
continue;
} else {
# Increment the count of each value
$hash[$value]++;
}
}
$items = $filtered_items;
将生成
数组(0=>3,3=>3,)
。在您的示例中,0和3重复3次。实际上,这里需要过滤结果数组并去掉必要的值,但您在这里使用的是正确的。ByRef数组而不是返回?我可以问一下原因吗?@Brad:我只是无法决定返回哪个变量$none
或$new\u array
,但最后我决定使用引用。如果不总是提供任何变量,但数组是,那么总是获得数组结果是否有意义?;-)(我不是怀疑,只是好奇)这个解决方案很好,很有魅力!非常感谢。哇,伙计们,这太好了,谢谢大家!所有的建议都非常棒,我试过几次,都很有魅力!!!你真的知道你在这里写的是什么:)接受了deceze的解决方案,因为它是最优雅的解决方案,但你们都很棒,要感谢你们的帮助!尊敬!
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$none=1;
$new_array=array();
$n=3;
dojob($items,$n,$none,$new_array);
function dojob($items,$n,&$none,&$new_array)
{
$values_count=array_count_values($items);
foreach($values_count as $value => $count)
{
if($count ==$n)
{
$none=0;
$new_array[]=$value;
}
}
}
<?php
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$temp = array_unique($items);
$result = array();
$none = 1;
$number = 3;
foreach($temp as $tmp)
{
if(count(array_keys($items, $tmp)) == $number)
{
array_push($result,$tmp);
$none = 0;
}
}
print_r($result);
?>
function array_repeats($items,$repeats,&$none){
$result = array();
foreach (array_unique($items) as $item){
$matches = array_filter($items,create_function('$a','return ($a=='.$item.');'));
if (count($matches) == $repeats)
$result[] = $item;
}
$none = (count($result)?1:0);
return $result;
}
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$number = 3;
$none = 1;
foreach ($items as $value) {
if ($hash[$value] >= $number) {
# This $value has occured as least $number times. Lets save it.
$filtered_items[] = $value;
# We have at least one item in the $items array >= $number times
# so set $none to 0
$none = 0;
# No need to keep adding
continue;
} else {
# Increment the count of each value
$hash[$value]++;
}
}
$items = $filtered_items;
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$icnt = array_count_values($items);
function eq3($v) {
return $v==3;
}
var_export(array_filter($icnt, 'eq3'));