Jeff
Jeff

Reputation: 6110

How to remove an element from a PHP array if the key equals 'index'?

I have this $array in which the index key could appear in any random order:

array(4) {
  ["foo"]=> bool(false)
  ["index"]=> bool(false)
  ["bar"]=> bool(true)
  ["biff"]=> bool(false)
}

Without adjusting the position of the elements or changing the key or value, how do I remove the index element, resulting in a new $array?

array(3) {
  ["foo"]=> bool(false)
  ["bar"]=> bool(true)
  ["biff"]=> bool(false)
}

Upvotes: 1

Views: 703

Answers (3)

David Pfeffer
David Pfeffer

Reputation: 39833

unset($array['index']); is what you're looking for. This will work even if there is no 'index' key in the array.

Upvotes: 3

Tomas Markauskas
Tomas Markauskas

Reputation: 11596

Use:

unset($array['index']);

Upvotes: 6

erenon
erenon

Reputation: 19118

unset($array['index']);

Upvotes: 4

Related Questions