Hi I am coding a system in which I need a function to get and remove the first element of the array. This array has numbers i.e.
0,1,2,3,4,5
how can I loop through this array and with each pass get the value and then remove that from the array so at the end of 5 rounds the array will be empty.
Thanks in advance
The array_shift() function removes the first element from an array, and returns the value of the removed element. Note: If the keys are numeric, all elements will get new keys, starting from 0 and increases by 1 (See example below).
shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
The array_keys() function returns an array containing the keys.
The array_shift() function is used to remove the first element from an array, and returns the value of the removed element. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
You can use array_shift
for this:
while (($num = array_shift($arr)) !== NULL) {
// use $num
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With