note 25559 modified in function.array-filter by vrana

From: Date: Tue, 17 Aug 2004 13:58:05 +0000
Subject: note 25559 modified in function.array-filter by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80037@lists.php.net to get a copy of this message
be careful with the above function "array_delete"'s use of the stristr function, it could be slightly misleading. consider the following: <?php 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); ?> --was-- 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); ?> http://php.net/manual/en/function.array-filter.php

« previous php.notes (#80037) next »