0
$fields = array('timbo', '22', 'norway');

How could I unset the array key for norway based on it's value alone?

Nick Shears
  • 1,053
  • 1
  • 10
  • 28

3 Answers3

7
array_diff($fields, array('norway'))

http://php.net/array_diff

deceze
  • 491,798
  • 79
  • 706
  • 853
1
$key = array_search( 'norway', $fields ); 
if ($key !== FALSE) {
    unset($fields[$key]); // remove 
}
0

You could use array_search to get the index. array_search will exit after the first match.

$index = array_search('norway', $fields);
if($index !== FALSE)
    unset($fields[$index]);
Tobias Golbs
  • 4,506
  • 3
  • 26
  • 49