When writing PHP applications, you may need to add one or more values to the beginning of an array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_unshift() 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_unshift() in PHP?
Add one or more values to the beginning of an array.
Its syntax is:
array_unshift(array &$array, mixed ...$values): int
Parameters used by this operation:
&$array— The collection being examined or updated.$values— The value supplied for this argument affects the operation.
The input changes, numeric keys are reindexed, and the function returns the new element count, not the new array.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$queue = ["Review", "Deploy"];
$size = array_unshift($queue, "Urgent fix");
print_r($queue);
echo $size;
Output:
Array
(
[0] => Urgent fix
[1] => Review
[2] => Deploy
)
3
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
$menu = ["Guides", "Contact"];
array_unshift($menu, "Home", "Tools");
print_r($menu);
Output:
Array
(
[0] => Home
[1] => Tools
[2] => Guides
[3] => Contact
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Place important notices before normal messages in an ordered list
Place important notices before normal messages in an ordered list. Save the code in a PHP file and adapt the sample data to your project.
<?php
$pendingTasks = ["process-export", "send-report"];
array_unshift($pendingTasks, "check-permissions");
echo "Next: " . $pendingTasks[0] . PHP_EOL;
echo "Queued: " . count($pendingTasks);
Output:
Next: check-permissions
Queued: 3
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 changes, numeric keys are reindexed, and the function returns the new element count, not the new array.
- 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_unshift() | Add one or more values to the beginning of an array |
| Related functions | array_push() adds to the end; array_shift() removes the first value. |
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 changes, numeric keys are reindexed, and the function returns the new element count, not the new array. |
| Choosing a related function with different behavior | array_push() adds to the end; array_shift() removes the first value. |
Key Takeaways
array_unshift()helps you add one or more values to the beginning of an array.- The input changes, numeric keys are reindexed, and the function returns the new element count, not the new array.
- array_push() adds to the end; array_shift() removes the first value.
- Use the practical example as a starting point, but validate real application inputs.
array_unshift() is useful when you need to add one or more values to the beginning of an array. Use its documented rules and the examples above to choose the right behavior for your data.