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

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

Nick Shears
  • 1,103
  • 1
  • 13
  • 30

3 Answers3

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

http://php.net/array_diff

deceze
  • 510,633
  • 85
  • 743
  • 889
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,586
  • 3
  • 28
  • 49