p0kerface Posted September 29, 2011 Share Posted September 29, 2011 Hello guys, need some help. Let us say i have an array = 2011 ipad 2011 ipad review 2011 ipad price 2011 ipad design 2011 ipad reviews review 2011 ipad 2011 ipad feature I would like to review all the array elements that contain "review"...So the final array should look like 2011 ipad 2011 ipad price 2011 ipad design 2011 ipad feature Thanks Quote Link to comment https://forums.phpfreaks.com/topic/248080-remove-certain-strings-from-array/ Share on other sites More sharing options...
mikesta707 Posted September 29, 2011 Share Posted September 29, 2011 function removeAt($array, $search){ $keys = array();//keys to remove foreach($array as $key=>$a){ if (strpos($array, $search)!== false){ //if we find the search term in the value, we set it for removal $keys[] = $key; } } foreach($keys as $k){ array_splice($array, $key);//remove the elements from the array } }//end removeAt //usage: $array = array(... some stuff ...); $array = removeAt($array, "review"); here you go. Please note that this uses a simple string match. If you want more comprehensive matching, you may want to use a regular expression match rather than the str_pos !== false method Quote Link to comment https://forums.phpfreaks.com/topic/248080-remove-certain-strings-from-array/#findComment-1273826 Share on other sites More sharing options...
p0kerface Posted September 29, 2011 Author Share Posted September 29, 2011 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248080-remove-certain-strings-from-array/#findComment-1273830 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.