How to Insert Elements at the Beginning of a PHP Array with array_unshift

This guide explains the PHP array_unshift function, showing its syntax, how to add one or multiple elements to the start of an array, and provides clear code examples with expected output for both string and numeric arrays.

php Courses
php Courses
php Courses
How to Insert Elements at the Beginning of a PHP Array with array_unshift

PHP is a widely used server‑side scripting language, and arrays are a core data structure for storing collections of values. When you need to prepend elements to an array, the built‑in array_unshift function is the most convenient tool.

The function inserts one or more values at the beginning of an array and returns the new number of elements. Its syntax is:

array_unshift(array $array, mixed $value1 [, mixed $value2 ...])

Here $array is the target array, and $value1, $value2, etc., are the values to prepend.

Example 1: Adding a fruit to a list

<?php
$fruits = array("apple", "orange", "banana");

echo "Before array_unshift: ";
print_r($fruits);

array_unshift($fruits, "grape");

echo "After array_unshift: ";
print_r($fruits);
?>

Running this script produces:

Before array_unshift: Array
(
    [0] => apple
    [1] => orange
    [2] => banana
)
After array_unshift: Array
(
    [0] => grape
    [1] => apple
    [2] => orange
    [3] => banana
)

The original array contained three fruits; after calling array_unshift, "grape" appears at index 0, shifting the existing elements forward.

Example 2: Prepending multiple numbers

<?php
$numbers = array(3, 4, 5);

echo "Before array_unshift: ";
print_r($numbers);

array_unshift($numbers, 1, 2);

echo "After array_unshift: ";
print_r($numbers);
?>

The output is:

Before array_unshift: Array
(
    [0] => 3
    [1] => 4
    [2] => 5
)
After array_unshift: Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

This demonstrates that array_unshift can add several values at once, placing them in the order provided.

In summary, array_unshift offers a simple way to prepend one or more elements to a PHP array without manually re‑indexing, making it useful for a variety of backend programming tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPphp-functionsarray manipulationarray_unshift
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.