Open In App

PHP array_push() Function

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This inbuilt function of PHP is used to push new elements into an array. We can push one or more than one element into the array and these elements gets inserted to the end of the array and because of the pushed elements into the array, the length of the array also gets incremented by the number of elements pushed into the array.

Syntax:

array_push($array, $val1, $val2, $val3....)

Parameters:
The function can take multiple parameters, depending on the number of elements we want to push into the array. We can classify the parameters in two categories as shown below:

  1. $array: This parameter refers to the original array we want to operate upon.
  2. List of values: This parameter refers to the list of elements separated by commas we want to push into the array. In the above syntax the list of values to be pushed is $val1, $val2, $val3….

Return Value: This function returns the modified array, with all the elements pushed to the end of the array.

Note: If the array has a key, value pair, then the method will always add a numeric key to the pushed value.

Examples:

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

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

Below programs illustrate the array_push() function in PHP:

  • In the below program the array_push() function is used to push new elements in an array with no keys.




    <?php
    // PHP code to illustrate the use of array_push()
      
    // Input array
    $array = array("ram", "krishna", "aakash");
      
    // elements to push
    $a1 = "rohan";
    $a2 = "rajeeb";
    $a3 = "saniya";
      
    // array after pushing new elements
    print_r(array_push($array, $a1, $a2, $a3));
    ?>

    
    

    Output:

    Array
    (
        [0] => ram
        [1] => krishna
        [2] => aakash
        [3] => rohan
        [4] => rajeeb
        [5] => saniya
    )
    
  • In the below program, we will understand how the array_push() function works with an array having a already defined key_value pair.




    <?php
    // PHP code to illustrate the use of array_push()
      
    // Input Array
    $array = array(1=>"ram", 2=>"krishna", 3=>"aakash");
      
    // Elements to push
    $a1 = "rohan";
    $a2 = "rajeeb";
    $a3 = "saniya";
      
    // Array after pushing new elements
    print_r(array_push($array, $a1, $a2, $a3));
    ?>

    
    

    Output:

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

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads