note 60822 modified in function.array-filter by thiago
| From: | thiago@php.net | Date: | Tue, 22 Jul 2008 23:34:08 +0000 |
| Subject: | note 60822 modified in function.array-filter by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142578@lists.php.net to get a copy of this message | ||
I have written a function that will filter an array by the frequency of
element value in the array. This may be useful to some people.
<?php
/////////////////////////////////////////////////////////////////
// Filter an array by value freguebcy
// Input: $array
// cut-off: $frequency (>=1)
// result option option: 1=$frequency and higher
// 0=$frequency only
// -1=$frequency and lower
/////////////////////////////////////////////////////////////////
function filter_array($array, $frequency = 2, $include = 1){
$freg = array_count_values($array);
if($frequency<1){
print "** frequency cut-off should be >= 1! **\n";
return false;
}
foreach($freg as $k => $v){
if($include == 0){
if($frequency != $v){
$freg[$k] = 0;
}
}elseif($include > 0){
if($frequency > $v){
$freg[$k] = 0;
}
}else{
if($frequency < $v){
$freg[$k] = 0;
}
}
}
$filtered = array_filter($freg);
$values = array_keys($filtered);
return array_intersect($array,$values);
}
?>
--was--
I have written a function that will filter an array by the frequency of
element value in the array. This may be useful to some people.
/////////////////////////////////////////////////////////////////
// Filter an array by value freguebcy
// Input: $array
// cut-off: $frequency (>=1)
// result option option: 1=$frequency and higher
// 0=$frequency only
// -1=$frequency and lower
/////////////////////////////////////////////////////////////////
function filter_array($array, $frequency = 2, $include = 1){
$freg = array_count_values($array);
if($frequency<1){
print "** frequency cut-off should be >= 1! **\n";
return false;
}
foreach($freg as $k => $v){
if($include == 0){
if($frequency != $v){
$freg[$k] = 0;
}
}elseif($include > 0){
if($frequency > $v){
$freg[$k] = 0;
}
}else{
if($frequency < $v){
$freg[$k] = 0;
}
}
}
$filtered = array_filter($freg);
$values = array_keys($filtered);
return array_intersect($array,$values);
}
http://php.net/manual/en/function.array-filter.php