Tutorialspoint.dev

PHP | array_unshift() Function

This inbuilt function of PHP is used to add on or more elements into an array and these elements are added to at the beginning of the array. All the elements that we add into the array are inserted in the same order, as they have been passed. They are numerically indexed starting from 0th position. If there are string keys, then they remain unchanged.

Syntax:

int array_unshift($array, $val1, $val2, $val3....)

Parameters:
The function can take multiple parameters, depending on the number of elements we want to insert into the array. We have basically classified the parameters in two categories as explained below:

  1. $array: This is a mandatory parameter and refers to the original array we want to operate upon.
  2. List_of_values: This is a group of parameters and represents a list of values we need to insert in the array, $array. In the above syntax the List_of_values is $val1, $val2, $val3…..

Return Value: This function returns the total number of elements in the new modified array after inserting elements.

Examples:



Input : $array = ("ram", "krishna", "aakash")
        $val1 = "rohan", $val2 = "rajeeb", $val3 = "saniya"
Output :
Array
(
    [0] => rohan
    [1] => rajeeb
    [2] => saniya
    [3] => ram
    [4] => krishna
    [5] => aakash
)

Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash")
        $val1 = "rohan", $val2 = "rajeeb", $val3 = "saniya";
Output :
Array
(
    [0] => rohan
    [1] => rajeeb
    [2] => saniya
    [3] => ram
    [4] => krishna
    [5] => aakash
)

Below programs illustrate the array_unshift() function in PHP:

  • In this program, we will try to understand the working of the function array_unshift() by adding the elements to the beginning of the array. We will also observe that the numeric keys are added automatically.

    <?php
      
    // PHP programm to illustrate 
    // the use of array_unshift()
      
    // Input Array
    $array = array("ram", "krishna", "aakash");
      
    // Values to be added
    $a1 = "rohan";
    $a2 = "rajeeb";
    $a3 = "saniya";
      
    // Callig array_unshift()
    array_unshift($array, $a1, $a2, $a3);
      
    // Print modified array
    print_r($array);
      
    ?>

    Output:

    Array
    (
        [0] => rohan
        [1] => rajeeb
        [2] => saniya
        [3] => ram
        [4] => krishna
        [5] => aakash
    )
  • In the above program we have seen that if a non-key array is passed to the array_unshift() function then it is automatically modified to array with numeric keys. But if the array already had numeric keys starting from zero then after inserting new elements the keys will get modified. Below program illustrate this:

    <?php
      
    // PHP program to illustrate 
    // the use of array_unshift()
      
    // Input Array
    $array = array(1=>"ram", 2=>"krishna", 3=>"aakash");
      
    // Values to be inserted
    $a1 = "rohan";
    $a2 = "rajeeb";
    $a3 = "saniya";
      
    // Calling array_unshift()
    array_unshift($array, $a1, $a2, $a3);
      
    // Print modified array
    print_r($array);
      
    ?>

    Output:

    Array
    (
        [0] => rohan
        [1] => rajeeb
        [2] => saniya
        [3] => ram
        [4] => krishna
        [5] => aakash
    )
    

Reference:
http://php.net/manual/en/function.array-unshift.php



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter