note 24477 modified in function.array-filter by vrana

From: Date: Tue, 17 Aug 2004 13:58:08 +0000
Subject: note 24477 modified in function.array-filter by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80040@lists.php.net to get a copy of this message
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): <?php 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); ?> --was-- 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); http://php.net/manual/en/function.array-filter.php

« previous php.notes (#80040) next »