Jump to content

Remove certain strings from array


p0kerface

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/248080-remove-certain-strings-from-array/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.