Open In App

How to insert an item at the beginning of an array in PHP ?

Last Updated : 21 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Arrays in PHP are a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. 

There are some methods to insert an item at the beginning of an array which are discussed below:

Using array_merge() Function

The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. 

  • Create an array containing array elements.
  • Create another array containing one element that needs to be inserted at the beginning of another array.
  • Use the array_merge() function to merge both arrays to create a single array.

Example: 

php
<?php

// Declare an array
$arr1 = array(
    "GeeksforGeeks",
    "Computer",
    "Science",
    "Portal"
);

// Declare another array containing
// element which need to insert at
// the beginning of $arr1
$arr2 = array(
    "Welcome"
);

// User array_merge() function to
// merge both array
$mergeArr = array_merge( $arr1, $arr2 );

print_r($mergeArr);

?>

Output
Array
(
    [0] => GeeksforGeeks
    [1] => Computer
    [2] => Science
    [3] => Portal
    [4] => Welcome
)

Using array_unshift() function

The array_unshift() function is used to add one or more elements at the beginning of the array. 

Example 1: 

php
<?php

// Declare an array
$array = array(
    "GeeksforGeeks",
    "Computer",
    "Science",
    "Portal"
);

// Declare a variable containing element
$element = "Welcome";

// User array_unshift() function to
// insert element at beginning of array
array_unshift( $array, $element );

print_r($array);

?>

Output
Array
(
    [0] => Welcome
    [1] => GeeksforGeeks
    [2] => Computer
    [3] => Science
    [4] => Portal
)

Example 2: 

php
<?php

// Declare an associative array
$array = array(
    "p" => "GeeksforGeeks",
    "q" => "Computer",
    "r" => "Science",
    "s" => "Portal"
);

// Declare a variable containing element
$element = "Welcome";

// User array_unshift() function to
// insert element at beginning of array
array_unshift( $array, $element );

print_r($array);

?>

Output
Array
(
    [0] => Welcome
    [p] => GeeksforGeeks
    [q] => Computer
    [r] => Science
    [s] => Portal
)

Using array_splice()

Using array_splice() to insert an item at the beginning of an array works by specifying the position (0) where the new element should be inserted, setting the length to 0 (no elements removed), and adding the new element, thus modifying the original array in place.

Example:

PHP
<?php

// Declare an associative array
$array = array(
    "p" => "GeeksforGeeks",
    "q" => "Computer",
    "r" => "Science",
    "s" => "Portal"
);

// Declare a variable containing element
$element = array("a" => "Welcome");

// Use array_splice() function to insert element at beginning of array
array_splice($array, 0, 0, $element);

print_r($array);

?>

Output
Array
(
    [0] => Welcome
    [p] => GeeksforGeeks
    [q] => Computer
    [r] => Science
    [s] => Portal
)

Using Array Destructuring

This method manually constructs a new array by specifying the element to be added first, followed by the elements of the original array. This way, the new element is inserted at the beginning of the array.

Example: In this example, the insertAtBeginning function takes an array and an element as parameters. It creates a new array with the specified element at the first position, followed by the elements of the original array using the array_merge function.

PHP
<?php
    // Function to insert an item at the beginning of an array
function insertAtBeginning($array, $element) {
    return array_merge([$element], $array);
}

// Example usage
$originalArray = [2, 3, 4, 5];
$newElement = 1;

$newArray = insertAtBeginning($originalArray, $newElement);
print_r($newArray);

?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Using array_pad() Function

The array_pad() function can also be used to insert an item at the beginning of an array. This function pads the array to the specified length with a value, either adding to the beginning or end.

Example: In this example we pads the array $array with the value 1 at the beginning, extending its length to ensure it starts with [1, 2, 3, 4].

PHP
<?php

$array = [2, 3, 4];
$newArray = array_pad($array, -(count($array) + 1), 1);
print_r($newArray); // Output: [1, 2, 3, 4]

?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Using the + Operator

The “+” operator in PHP merges two arrays into a single array, preserving keys from the first array. When inserting an item at the beginning of an array, the “+” operator concatenates the new item with the original array, placing the new item at index zero.

Example

PHP
<?php
$array = array("banana", "cherry");

// Insert "apple" at the beginning using the + operator
$array = array("apple") + $array;

print_r($array);
?>

Output
Array
(
    [0] => apple
    [1] => cherry
)

Using a Combination of array_reverse() and array_push()

This method involves reversing the original array, adding the new element at the end using array_push(), and then reversing the array again to restore the original order with the new element at the beginning.

Steps:

  1. Reverse the original array using array_reverse().
  2. Use array_push() to add the new element to the end of the reversed array.
  3. Reverse the array again to get the final array with the new element at the beginning.

Example:

PHP
<?php

// Declare an array
$array = array(
    "GeeksforGeeks",
    "Computer",
    "Science",
    "Portal"
);

// Declare a variable containing element
$element = "Welcome";

// Reverse the array
$reversedArray = array_reverse($array);

// Add the element to the end of the reversed array
array_push($reversedArray, $element);

// Reverse the array again to restore original order
$finalArray = array_reverse($reversedArray);

print_r($finalArray);

?>

Output
Array
(
    [0] => Welcome
    [1] => GeeksforGeeks
    [2] => Computer
    [3] => Science
    [4] => Portal
)


Next Article

Similar Reads