Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove string from PHP array?

Tags:

Is it possible to remove a string (see example below) from a PHP array without knowing the index?

Example:

array = array("string1", "string2", "string3", "string4", "string5"); 

I need to remove string3.

like image 235
Zac Brown Avatar asked Nov 08 '10 01:11

Zac Brown


1 Answers

$index = array_search('string3',$array); if($index !== FALSE){     unset($array[$index]); } 

if you think your value will be in there more than once try using array_keys with a search value to get all of the indexes. You'll probably want to make sure

EDIT:

Note, that indexes remain unchanged when using unset. If this is an issue, there is a nice answer here that shows how to do this using array_splice.

like image 181
GWW Avatar answered Sep 28 '22 13:09

GWW