array_shift
array_shift(array &$array): mixed
Removes and returns the first element of the array, shifting the remaining elements one position forward.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *queue[] = {"a", "b", "c"};
int size = 3;
const char *first = queue[0];
memmove(queue, queue + 1, (size - 1) * sizeof(char *));
size--;
printf("%s\n", first);
printf("size=%d %s\n", size, queue[0]);
return 0;
}
a size=2 b
C++ C++26
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> queue{"a", "b", "c"};
std::string first = queue.front();
queue.erase(queue.begin());
std::cout << first << "\n";
std::cout << queue.size() << " " << queue.front() << "\n";
return 0;
}
a 2 b
C# v11.0.100
var queue = new List<string> { "a", "b", "c" };
string first = queue[0];
queue.RemoveAt(0);
Console.WriteLine(first);
Console.WriteLine($"{queue.Count} {queue[0]}");
a 2 b
Go v1.27.4
package main
import "fmt"
func main() {
queue := []string{"a", "b", "c"}
first := queue[0]
queue = queue[1:]
fmt.Println(first)
fmt.Println(len(queue), queue[0])
}
a 2 b
Java v27.0.1
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> queue = new ArrayList<>(List.of("a", "b", "c"));
String first = queue.remove(0);
System.out.println(first);
System.out.println(queue.size() + " " + queue.get(0));
}
}
a 2 b
JavaScript v26.13.1
const queue = ["a", "b", "c"];
const first = queue.shift();
console.log(first, queue);
a [ 'b', 'c' ]
PHP v8.6.0
$queue = ["a", "b", "c"];
$first = array_shift($queue);
echo $first, "\n", json_encode($queue);
a ["b","c"]
Python v3.15.2
queue = ["a", "b", "c"]
first = queue.pop(0)
print(first, queue)
a ['b', 'c']
Rust v1.99.1
fn main() {
let mut queue = vec!["a", "b", "c"];
let first = queue.remove(0);
println!("{} {:?}", first, queue);
}
a ["b", "c"]
TypeScript v26.13.1
const queue: string[] = ["a", "b", "c"];
const first: string | undefined = queue.shift();
console.log(first, queue);
a [ 'b', 'c' ]
Language differences
C: idiomatically uses memmove to shift the remaining elements forward.
Go: the slice reslice s[1:] is the only O(1) form — data is not moved, only the sliding window moves, yet the head of the backing array is never freed, so long-lived queues that shift repeatedly accumulate memory and need periodic copies or a ring buffer.
Java: ArrayList.remove(0) is likewise O(n) — production code reaches for ArrayDeque/LinkedList.
PHP: removing from the head is O(n) — every element but Go's must shift forward; array_shift and JS/TS shift share the same name and semantics.
Python: the counterpart is pop(0), but it is slow on lists, so queue code idiomatically uses collections.deque.popleft() (O(1)).
Rust: Vec::remove(0) shifts the whole tail; high-throughput code switches to VecDeque::pop_front.