note 33517 modified in function.array-filter by vrana

From: Date: Tue, 17 Aug 2004 13:58:00 +0000
Subject: note 33517 modified in function.array-filter by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80034@lists.php.net to get a copy of this message
Hi all, Here's a function that will look trough an array, and removes the array member when the search string is found. <?php function array_clean ($input, $delete = false, $caseSensitive = false) { $i = 0; while($i < count($input)) { if($delete) { if($caseSensitive) { if(!strstr($input[$i] ,$delete)) { $return[] = $input[$i]; } } else { if(!stristr($input[$i], $delete)) { $return[] = $input[$i]; } } } else { if(!empty($input[$i])) { $return[] = $input[$i]; } } $i++; } return $return; } ?> array array_clean(array input [, string needle [, boolean case sensitive]]) if needle is left empty, the function will delete the array members that have no value (this means if it's empty). NOTE: It rebuilds the array from scratch, so keys begin with 0, like you would create a new array. Example: $array = array("John", "Doe", "Macy"); $array = array_clean($array, "doe", false); print_r($array); would return: array ( [0] => John [1] => Macy ) Hopes this helps someone :-) --was-- Hi all, Here's a function that will look trough an array, and removes the array member when the search string is found. function array_clean ($input, $delete = false, $caseSensitive = false) { $i = 0; while($i < count($input)) { if($delete) { if($caseSensitive) { if(!strstr($input[$i] ,$delete)) { $return[] = $input[$i]; } } else { if(!stristr($input[$i], $delete)) { $return[] = $input[$i]; } } } else { if(!empty($input[$i])) { $return[] = $input[$i]; } } $i++; } return $return; } array array_clean(array input [, string needle [, boolean case sensitive]]) if needle is left empty, the function will delete the array members that have no value (this means if it's empty). NOTE: It rebuilds the array from scratch, so keys begin with 0, like you would create a new array. Example: $array = array("John", "Doe", "Macy"); $array = array_clean($array, "doe", false); print_r($array); would return: array ( [0] => John [1] => Macy ) Hopes this helps someone :-) http://php.net/manual/en/function.array-filter.php

« previous php.notes (#80034) next »