soma56 Posted June 12, 2010 Share Posted June 12, 2010 I can see how we can remove specific variables within an array: <?php // Provides: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); ?> What if we wanted to completely remove variables based on only a partial variable name? <?PHP $animals_of_php = array("Angry Dog", "Smiley Cat", "Mad Moose", "Nice Dog", "Nice Cat", "Ticked Off Flea", "Psycho Mouse", "Happy Skunk", "Laughing Ants", "Snappy Snake"); $only_nice_animals = ?? ?> Would it be smarter to filter out what you want before the array is created or simply filter the array after it's been created? What I'm trying to figure out, in a hopefully entertaining way, is what type of function is needed in providing only partial variables: "Angry", "Mad", "Ticked", "Psycho" and "Snappy" - resulting in removing the entire variable: "Angry Dog", "Ticked Off Flea", "Psycho Mouse", "Snappy Snake" Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/ Share on other sites More sharing options...
soma56 Posted June 12, 2010 Author Share Posted June 12, 2010 I'll try and be more specific if it helps. Let's say I knew in advanced what the a partial name of the variable within the array that I don't want. Is that enough information to unset the variable? I don't have a use for the word "Bad" If my array comes in with "Bad Dog", "Bad Cat", "Bad Bear" - what's the best way to, not only the word "Bad", but whatever else follows within a list? Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1071246 Share on other sites More sharing options...
Alex Posted June 12, 2010 Share Posted June 12, 2010 You mean something like this?: $arr = array("Bad Dog", "Bad Cat", "Bad Bear", "Nice Bear", "Nice Dog"); foreach($arr as $key => $val){ if(strpos(strtolower($val), 'bad') !== false){ unset($arr[$key]); } } print_r($arr); Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1071249 Share on other sites More sharing options...
soma56 Posted June 12, 2010 Author Share Posted June 12, 2010 Nice, I've found a way to make it work, but what about adding more conditions? <?PHP if(strpos(strtolower($val), 'bad') !== false){ ?> Perhaps an array in that spot? <?PHP $angry_animals = array('bad', 'ugly','mad'); if(strpos(strtolower($val), '$angry_animals') !== false){ ?> i haven't tried it yet but am I on the right track? Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1071265 Share on other sites More sharing options...
Alex Posted June 12, 2010 Share Posted June 12, 2010 Make a function like this: function strpos_array($haystack, $search){ foreach($search as $term){ if(strpos($haystack, $term) !== false){ return true; } } return false; } Then use it like this: $arr = array("Bad Dog", "Bad Cat", "Bad Bear", "Nice Bear", "Nice Dog"); $angry_animals = array('bad', 'ugly', 'mad'); foreach($arr as $key => $val){ if(strpos_array($val, $angry_animals)){ unset($arr[$key]); } } print_r($arr); Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1071269 Share on other sites More sharing options...
soma56 Posted June 12, 2010 Author Share Posted June 12, 2010 It makes sense and works. I thank both of you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1071280 Share on other sites More sharing options...
soma56 Posted July 7, 2010 Author Share Posted July 7, 2010 For anyone that needs to have this done as far as case insensitive goes I figured it out: function strpos_array($haystack, $search){ foreach($search as $term){ if(stristr($haystack, $term) !== false){ return true; } } return false; } $arr = array("Bad Dog", "Bad Cat", "Bad Bear", "Nice Bear", "Nice Dog"); $angry_animals = array('bad', 'ugly', 'mad'); foreach($arr as $key => $val){ if (strpos_array($val, $angry_animals)){ echo $val; } } I simply replaced 'strpos' with 'stristr'. The code above echoes out the 'angry_animals'. Thanks again for everyone's help on this one. Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1082692 Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 strpos() (and its alternatives such as stristr()) may not be appropriate for this usage since they do not know when "bad" is a whole word in the string ("bad dog") vs. part of another word, ("good badger"). That function would incorrectly filter out "good badger" because of the "bad" in the word "badger". In this case you will want to use a regular expression. This is a little more flexible. The function takes the input array, an array of words to use for filtering, and an optional third parameter to determine whether you want the return value to be the array of values that are not filtered(default) or the array fo the filtered values. function filterArrayByWords($inputAry, $excludeWordsAry, $returnInclude=true) { $excludeAry = array(); $includeAry = $inputAry; foreach($includeAry as $keyInt => $valueStr) { foreach($excludeWordsAry as $excludeWordStr) { //Check for exclude word using word boundrys if(preg_match("/\b{$excludeWordStr}\b/i", $valueStr)!=0) { $excludeAry[$keyInt] = $valueStr; unset($includeAry[$keyInt]); break; } } } return ($returnInclude) ? $includeAry : $excludeAry; } $animals = array("Bad Dog", "Bad Cat", "Bad Bear", "Nice Bear", "Nice Dog", "Good Badger"); $angry_animals = array('bad', 'ugly', 'mad'); print_r(filterArrayByWords($animals, $angry_animals)); //Output: Array ( [3] => Nice Bear [4] => Nice Dog [5] => Good Badger ) print_r(filterArrayByWords($animals, $angry_animals, false)); //Output: Array ( [0] => Bad Dog [1] => Bad Cat [2] => Bad Bear ) Quote Link to comment https://forums.phpfreaks.com/topic/204598-removing-angry-animal-variables-from-array/#findComment-1082706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.