如何对数组值进行计数并只保留那些相关的值?
我有一个包含多个整数的数组,我只对重复自己一定次数的整数感兴趣。例如:如何对数组值进行计数并只保留那些相关的值?
$items = (0, 0, 0, 1, 1, 2, 3, 3, 3)
我想知道哪些项目(S)被原原本本地重复$number
(在这个例子中$number = 3
)倍(在这个例子中new array $items = (0, 3)
)。
如果没有任何数组项重复$number
次,我需要有var $none = 1
。
我知道一个功能array_count_values但不知道如何将其落实到我的情况......
$number = 3;
$items = array_keys(array_filter(array_count_values($items), create_function('$n', "return \$n == $number;")));
if (!$items) {
$none = 1;
}
- 使用
array_count_values
得到的每个数字是如何经常通过发生 - 过滤器这样的配对
array_filter
回调,丢弃除了那些计数为的所有条目 - 采取所得阵列的键(实际计数值)
- 所得阵列为空或者包含发生的次
不错的工作。非常聪明。 – Ben 2011-04-26 13:39:17
$ repeated_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
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$count = array_count_values($items);
$number = 3;
$none = 1;
$result = array();
foreach(array_unique($items) as $item) {
if($count[$item] == $number) {
$result[] = $item;
$none = 0;
}
}
这个解决方案非常好,像魅力一样工作!谢谢! – marko 2011-04-26 14:02:35
$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;
}
}
}
by-ref数组而不是返回?请问为什么? – 2011-04-26 13:37:37
@Brad:我只是无法确定哪个变量返回'$ none'或'$ new_array',但最后我决定使用引用。 – 2011-04-26 13:40:20
鉴于没有总是提供,但数组是,总是有数组结果没有意义吗? ;-)(只是为了清楚我不是怀疑,只是很好奇) – 2011-04-26 13:42:43
$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);
?>
我知道有很多解决方案,但认为我会增加一项。 ;-)
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'));
会产生array (0 => 3, 3 => 3,)
。在你的例子0和3中重复3次。在这里需要Array_filter,实际上,过滤您的结果数组并删除必要的值,但您在这里使用array_count_values是正确的。
你的解决方案太:)谢谢你! – marko 2011-04-26 14:01:46
哇,这很好,谢谢大家!所有的建议都非常棒,尝试过一对夫妻,所有的工作都像一个魅力!你真的知道你在这里写什么:))接受了欺骗的解决方案,因为它是最优雅的一个,但你们都很棒,想要感谢你的帮助!尊重! – marko 2011-04-26 14:03:09