All the numerical array keys will be modified khổng lồ start the counting from zero while the literal keys won’t be touched. If an array is empty, then the Null is returned.

Bạn đang xem: Php array_shift

PHP array shift

PHP array_shift() is a built-in function that removes the first element from an array và returns the value of a removed element. The array-shift() function accepts an array as an argument và returns the removed value.



An array parameter is required, & it specifies an array.


See the below code example.

See the output.


An array_shift() function in Associative Array

See the following code example of an associative array.

'Apple', 'm' => 'Microsoft', 'b' => 'Amazon', 'c' => 'Alphabet', 'f' => 'Facebook'>;echo array_shift($data)." ";print_r($data);See the output.

Xem thêm: Mẹ Bầu 3 Tháng Đầu Ăn Thịt Vịt Được Không Nên Ăn Gì? Có Tốt Cho Em Bé Không


Using array_shift over an enormous array was relatively slow. However, it sped up as an array shrank và reindexed a smaller data set.

For that purpose, you can use the array_reverse, then array_pop, which doesn’t need to lớn reindex an array and will preserve the keys if you want it to.

The complexity of an array_pop() is O(1). The complexity of an array_shift() is O(n). An array_shift() requires a re-index process on the array, so it has to run over all the elements & index them.

After removing the first element, the key of the remaining items is modified và again renumbered from the start, only if the keys are numerical. 

So, if you want to lớn remove the first element from an array, you can use the array_shift() function.

That’s it for this tutorial.

Xem thêm: Bà Bầu Bị Ợ Hơi Phải Làm Sao, Bà Bầu Bị Ơ Chua Phải Làm Sao


Krunal Lathiya is an Information technology Engineer. By profession, he is a website developer with knowledge of multiple back-end platforms (e.g., PHP, Node.js, Python) và frontend JavaScript frameworks (e.g., Angular, React, and Vue).