Latest Code Tutorials

PHP array push: How to Add Elements to Array

PHP add to array is an operation in which we append elements to the existing array. An array can contain many values under a single name, and you can access the values by referring to an index number.

In PHP, there are three types of array.

  1. Indexed arrays – Arrays with the numeric index.
  2. Associative arrays – Arrays with the named keys.
  3. Multidimensional arrays – Arrays that contain one or more arrays.

Now, let’s see an example where we perform PHP to add array operations.

PHP array push

PHP array_push() is a built-in function used to insert new elements at the end of an array and get the updated array elements. The array_push() method takes a single element or an array of elements and appends it to the array.

You may add as many values as you need. Your inserted elements will always have numeric keys, even if the array itself has string keys. PHP array push() function has been introduced in PHP 4.

If we want to add more values to a PHP array, we need to use the array_push() function, which inserts one or more elements to the end of an array.

The length of the array increases by the number of variables pushed. You can add one element or multiple elements at a time using the array_push() function.

The array_push() treats an array as a stack and pushes the passed variables onto an array’s end. 


The syntax for PHP Array Push is the following.



An array parameter is required in which we will add the value.

The value1 parameter is also required, which is the value we will insert into the specified array.

The value2, value3, and so on are optional parameters. However, we need to pass those parameters if we want to add multiple values.

Return value

The array_push() function will return the length of new elements of an array.


Let’s see an example.

Create one file called app.php and add the following code.


// app.php

$netflix = ['Stranger Things', 'Black Mirror', 'Bright', 'XOXO'];

$new = array_push($netflix, 'Shaft');

echo $new."\n";

In the above code, we have defined one array called $netflix, which has four items.

We have added the fifth item using the array_push() function in PHP and then printed the original array and the return value from the array_push function.

Now, we are running the file on the terminal. So go to the terminal, navigate the app.php file directory, and type the following command to run the file.

php app.php

See the output.

PHP Array Push Example | Add Elements To An Array

That means we have successfully added the Shaft show to the $neflix array.

Also, the array_push() function returns the length of the array. In our case, it is 5. Remember, the PHP Array index starts from 0.

This operation is also called PHP add to the array.

Adding multiple values to PHP array

To add multiple values in PHP array, use the array_push() function. The array_push() function takes multiple elements and appends all the elements into the array. It will add in the order that they are added. It does not change its order.


// app.php

$netflix = ['Stranger Things', 'Black Mirror', 'Bright', 'XOXO'];

$new = array_push($netflix, 'Shaft', 'Mute', 'Clinical', 'Blue Jay', 'Candy Jar');

echo $new."\n";

The output is the following.

Add Multiple Values To An Array in PHP

Adding values to the Associative Array

To add values in an associative array in PHP, use the array_push() function. The array_push() function takes single or multiple arguments and returns the associative array.

Let’s take a scenario where we add values to the Associative Array.


// app.php

$data = ['name' => 'Krunal', 'education' => 'BE'];
$new = array_push($data, 'Ankit', 'MCA');

echo $new."\n";

See, the $data variable is Associative Array, and we have added two values to that array.

That means the first two items are associative, which have their key. But, from 3rd and 4th, they have indexes starting from 0. So, let’s run the PHP file and see the output.

Add Values to the Associative Array in PHP

Adding array into an array in PHP

To add an array into an array in PHP, use the array_push() function. The array_push() function takes an array as an argument and returns the array combining with old and new values.

Okay, now let’s take a scenario where we add a whole array inside an array and see the output.


// app.php

$dataA = ['name' => 'Krunal', 'education' => 'BE'];
$second = ['Facebook', 'Instagram'];
$newA = array_push($dataA, $second);

echo $newA."\n";

The output is the following.

Add Array in the Array

See, it has added an array as a 3rd element, and it has its index, which is and 1.

So, right now, the dataA array is a multidimensional array.

PHP array length

To check array length in PHP, use the count() function. The count() method returns the total number of elements in the array. The sizeof() is an alias of the master function count().


$netflix = ['Stranger Things', 'Black Mirror', 'Bright', 'XOXO'];

echo count($netflix);

See the following output.

➜  pro php app.php
➜  pro

That means we have four elements in the $netflix array. If we add more items inside the array, then the size of the array increases, and if we use the array_pop() function, it will decrease the array length by one.

Pushing key and value in Associative Array

There is no array_push() equivalent for associative arrays because there is no way to determine the next key. We can use the array_push() method, but adding the index starts from 0 and 1, not the keys we desire. So if you want to push key and value, you can do the following code.


$data = ['name' => 'Krunal', 'education' => 'BE']; 

$data['age'] = 26;
$data['business'] = 'IT';


    [name] => Krunal
    [education] => BE
    [age] => 26
    [business] => IT

In the output, you can see that, and we can add multiple keys of your choice and not the ones that numeric keys php provides by default.

Pushing a value into the array automatically creates a numeric key for it. Therefore, when inserting a key-value pair to the array, you already have the key, and you don’t need one to be created for you. That key is the numeric key, which starts from 0.

Adding element at the start of the Array

To add an element at the start of the array, you can use the PHP array_unshift() function. It appends the item at the beginning of the array at the index of 0.


$data = ['Python', 'Javascript', 'Golang'];
array_unshift($data, 'PHP');


    [0] => PHP
    [1] => Python
    [2] => Javascript
    [3] => Golang

You can see that our new element “PHP” is added at the index position 0. 

The array_unshift() function adds new elements to the array. The new array values will be inserted at the beginning of the array. You can insert one value or as many as you like. Numeric keys will start at 0 and increase by 1 every time a new element is added. String keys will remain the same.

That’s it for PHP array_push() function.

Recommended Posts

PHP array_product()

PHP array_reverse()

PHP array_sum()

PHP array_walk()

PHP array_replace()

  1. goenergia says

    Thanks u help me.

  2. hk keluaran says

    Thank you for any other informative site. The place else may just I get that type of information written in such
    an ideal means? I’ve a undertaking that I am just now running on, and I’ve been on the glance out for such information.

  3. GARAMPOKER says

    If you are going for finest contents like I do,
    simply pay a visit this web site all the time since it provides quality contents,

  4. Malcolm says

    Awesome! Its really amaziing paragraph, I have got much clesr idea
    about from this piece of writing.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.