PHP Array Push: How to Add Elements to an Array

Introduction

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 arrays.

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

Understanding PHP array_push() function

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. However, your inserted elements will always have numeric keys, even if the array has string keys. 

PHP array_push() function has been introduced in PHP 4.

To add more values to a PHP array, you can 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 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. 

Syntax

The syntax for PHP Array Push is the following.

array_push(array,value1,value2...)

Parameters

An array parameter is required to 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 must pass those parameters to add multiple values.

Return value

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

Implenetation of array_push() method

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

<?php

// app.php

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

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

print_r($netflix);
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.

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.

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 the PHP array

To add multiple values in the PHP array, you can 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.

<?php

// app.php

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

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

print_r($netflix);
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.

<?php

// app.php

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

print_r($data);
echo $new."\n";

The $data variable is an 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 the 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 an 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.

Let’s take a scenario where we add a whole array inside an array and see the output.

<?php

// app.php

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

print_r($dataA);
echo $newA."\n";

The output is the following.

Add Array in the Array

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

Right now, the dataA array is a multidimensional array.

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.

<?php

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

$data['age'] = 26;
$data['business'] = 'IT';
print_r($data);

Output

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

In the output, you can see that we can add multiple keys of your choice, 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 into the array, you already have the key and don’t need one to be created for you.

That key is the numeric key, which starts from 0.

Adding an 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.

<?php

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

Output

Array
(
    [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.

Append elements using square brackets syntax in PHP

If you are using PHP version 5.4 or higher, you can use the square bracket syntax to add elements to an array like this:

$phones[] = 'iphone';
$phones[] = 'galaxy';

Pros and Cons of array_push() function

Here are some pros and cons of using the array_push() function in PHP:

Pros:

  1. The array_push() function is easy to use and understand.
  2. The array_push() function can add one or more elements to an array in a single function call.
  3. Moreover, it returns the new number of items in the array, which can be helpful in many cases.

Cons:

  1. The array_push() function requires you to pass the array by reference, which can confuse beginners.
  2. The array_push() function is slower than using the square bracket syntax to add elements to an array, especially if you add many items.
  3. If you are planning to add elements to the beginning of an array, it can be less efficient because it has to reindex all the elements to make room for the new elements.

Conclusion

Use PHP’s array_push() function to add or append single or multiple elements to an array.

After appending an element to an array, the length of an array will be changed!

4 thoughts on “PHP Array Push: How to Add Elements to an Array”

  1. 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.

    Reply
  2. 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,
    thanks

    Reply

Leave a Comment

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