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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.