lisovaccaro
lisovaccaro

Reputation: 34016

How to delete key from array and update index?

$array = array('a', 'b','c');
unset($array[0]);
var_dump($array);

Yields:
array(1) {
  [1]=>
  'b'
  'c'
}

How do I, remove array[0] to get ['bb','cc'] (no empty keys):

array(1) {
  'b'
  'c'
}

Upvotes: 7

Views: 30444

Answers (2)

Zhafur
Zhafur

Reputation: 1624

Check this:

$array = array('a', 'b','c');
unset($array[0]);
$array = array_values($array); //reindexing

Upvotes: 18

VoteyDisciple
VoteyDisciple

Reputation: 37813

Take a look at array_splice()

$array = array_splice($array, 0, 1);

If you happen to be removing the first element specifically (and not an arbitrary element in the middle of the array), array_shift() is more appropriate.

Upvotes: 13

Related Questions