When writing PHP applications, you may need to remove and return the first element in an array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_shift() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is array_shift() in PHP?
Remove and return the first element in an array.
Its syntax is:
array_shift(array &$array): mixed
Parameters used by this operation:
&$array— The collection being examined or updated.
The input is modified. Numeric keys are renumbered, string keys remain. An empty array returns null.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$tasks = ["email", "review", "deploy"];
echo array_shift($tasks) . PHP_EOL;
print_r($tasks);
Output:
email
Array
(
[0] => review
[1] => deploy
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$queue = ["first" => "Maya", "second" => "Noah"];
var_dump(array_shift($queue));
print_r($queue);
Output:
string(4) "Maya"
Array
(
[second] => Noah
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Process the next pending task in a small FIFO queue
Process the next pending task in a small FIFO queue. Save the code in a PHP file and adapt the sample data to your project.
<?php
$pendingJobs = ["generate-report", "send-notification", "clean-temp-files"];
while ($pendingJobs !== []) { echo "Running: " . array_shift($pendingJobs) . PHP_EOL; }
Output:
Running: generate-report
Running: send-notification
Running: clean-temp-files
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
The input is modified. Numeric keys are renumbered, string keys remain. An empty array returns null.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
array_shift() | Remove and return the first element in an array |
| Related functions | array_pop() removes the last element; array_unshift() adds elements to the front. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming input and result have identical keys | Check whether the function reindexes keys or retains original associations. |
| Confusing mutation with a returned copy | The input is modified. Numeric keys are renumbered, string keys remain. An empty array returns null. |
| Choosing a related function with different behavior | array_pop() removes the last element; array_unshift() adds elements to the front. |
Key Takeaways
array_shift()helps you remove and return the first element in an array.- The input is modified. Numeric keys are renumbered, string keys remain. An empty array returns null.
- array_pop() removes the last element; array_unshift() adds elements to the front.
- Use the practical example as a starting point, but validate real application inputs.
array_shift() is useful when you need to remove and return the first element in an array. Use its documented rules and the examples above to choose the right behavior for your data.