How to remove a key from an array and update index?

$array = array('a', 'b','c'); unset($array[0]); var_dump($array); Yields: array(1) { [1]=> 'b' 'c' } 

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

 array(1) { 'b' 'c' } 
+6
source share
2 answers

Check this:

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

Take a look at array_splice()

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

If you accidentally deleted the first element (and not an arbitrary element in the middle of the array), array_shift() more appropriate.

+13
source

Source: https://habr.com/ru/post/921093/


All Articles