|
|
 |
array_filter (PHP 4 >= 4.0.6) array_filter -- Filtre les éléments d'un tableau Descriptionarray array_filter ( array input [, mixed callback])
array_filter() retourne un tableau
contenant les éléments du tableau input,
filtrés grâce à la fonction callback.
Si input est un tableau associatif,
les clés sont préservées.
Exemple 1. Exemple avec array_filter() <?php
function pair($var) {
return ($var % 2 == 1);
}
function impair($var) {
return ($var % 2 == 0);
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
$tableau_pair = array_filter($array1, "pair");
$tableau_impair = array_filter($array2, "impair");
?> |
|
Cet exemple montre comment extraire les nombres pairs
dans $tableau_impair : ce dernier contient
array ("a"=>1, "c"=>3, "e"=>5);,
et les nombres impairs dans $tableau_pair :
ce dernier contient array (6, 8, 10, 12);,
Voir aussi
array_map() et
array_reduce().
User Contributed Notes array_filter |
 |
fgilles at free dot fr
13-Jul-2001 06:03 |
|
dub4u, your function doesn't truly replace array_filter() because it
doesn't restore originals $array keys.
Here is a replacement for
array_filter() for PHP4 < 4.0.6:
function
array_filter_it($array, $callback)
{
$farray = array ();
while(list($key,$val) = each($array))
if ($callback($val))
$farray[$key] = $val;
return $farray;
}
|
|
sam,pointsystems,com
21-Feb-2002 12:12 |
|
Here's a good function to filter multidimensional
arrays:
<code> function array_filter_multi($input,
$filter="", $keepMatches=true) { if
(!is_array($input)) return ($input==$filter xor
$keepMatches==false) ? $input : false; while (list
($key,$value) = @each($input)){ $res =
array_filter_multi($value, $filter,$keepMatches); if
($res !== false) $out[$key] = $res;
} return $out; } </code>
Default behavior is
to remove blanks from a multi-dimensional array, but you can filter out
any string (arg #2) with a positive or negative filter (arg #3).
|
|
jonathan at inetz dot com
24-Feb-2002 12:13 |
|
Leveraging PHP functions as callbacks can be especially useful. A handy
one is strlen(), as you can use it to filter out entries that don't have
values. (Sometimes if $HTTP_POST_VARS is huge, I like to limit it down to
those entries that were actually filled.)
$post_filtered =
array_filter( $HTTP_POST_VARS, "strlen" );
Use trim() if
some entries have whitespace:
$post_filtered = array_filter(
$str_array, "trim" );
|
|
|
|
|
13-Jun-2002 11:14 |
|
I was looking for a function able to take some values out of an array
iteratively, and found array_filter very useful although i had some
trouble figuring out the proper syntax...
class someclass {
var $current;
/** this is our iterative function */
function main ($variable,$array){ if (end test){
return true; } $variable= some treatment...
if (in_array($variable, $array)){
$this->current=something...($variable); // this is the
not-well-documented part $array=$array_filter($array,
array($this, "array_reduce"); }
$this->main($variable, $array); } /** this is the
function used to filter */ function reduce_list($var){
return ($var!=$this->current); } }
|
|
ajohnson at speakeasy dot org
17-Aug-2002 08:04 |
|
I was looking for a function to delete values from an array and thought I
had found it in array_filter(), however, I *didn't* want the keys to be
preserved *and* I needed blank values cleaned out of the array as well. I
came up with the following (with help from many of the above
examples):
function array_delete($array, $filterfor){
$thisarray = array (); foreach($array as $value)
if(stristr($value, $filterfor)===false && strlen($value)>0)
$thisarray[] = $value; return $thisarray; }
$array1 =
array ('OtHeR','this', 'that', 'Other','', 9, 101, 'fifty',
'other','','');
echo "<pre>array
:\n"; print_r($array1);
$array2=array_delete($array1,
"Other");
echo
"filtered:\n"; print_r($array2);
|
|
ajohnson at speakeasy dot org
27-Sep-2002 07:42 |
|
be careful with the above function "array_delete"'s
use of the stristr function, it could be slightly misleading. consider the
following:
function array_delete($array,
$filterforsubstring){ $thisarray = array (); foreach($array as
$value) if(stristr($value, $filterforsubstring)===false &&
strlen($value)>0) $thisarray[] = $value; return
$thisarray; }
function array_delete2($array, $filterforstring,
$removeblanksflag=0){ $thisarray = array (); foreach($array as
$value) if(!(stristr($value, $filterforstring) &&
strlen($value)==strlen($filterforstring)) &&
!(strlen($value)==0 && $removeblanksflag)) $thisarray[] =
$value; return $thisarray; }
function array_delete3($array,
$filterfor, $substringflag=0, $removeblanksflag=0){ $thisarray = array
(); foreach($array as $value) if( !(stristr($value,
$filterfor) && ($substringflag ||
strlen($value)==strlen($filterfor)) ) &&
!(strlen($value)==0 &&
$removeblanksflag) ) $thisarray[] = $value; return
$thisarray; }
$array1 = array ('the OtHeR thang','this', 'that',
'OtHer','', 9, 101, 'fifty', ' oTher', 'otHer
','','other','Other','','other blank things');
echo
"<pre>array
:\n"; print_r($array1);
$array2=array_delete($array1,
"Other");
echo "array_delete(\$array1,
\"Other\"):\n"; print_r($array2);
$array2=array_delete2($array1,
"Other");
echo "array_delete2(\$array1,
\"Other\"):\n"; print_r($array2);
$array2=array_delete2($array1,
"Other",1);
echo "array_delete2(\$array1,
\"Other\",1):\n"; print_r($array2);
$array2=array_delete3($array1,
"Other",1);
echo "array_delete3(\$array1,
\"Other\",1):\n"; print_r($array2);
$array2=array_delete3($array1,
"Other",0,1);
echo "array_delete3(\$array1,
\"Other\",0,1):\n"; print_r($array2); ?>
|
|
 |
| |