PHP Array Push Example | Add Elements To An Array
PHP Array Push Example | array_push() Tutorial is today’s topic. An array stores multiple values in one single variable. An array is a particular variable, which can hold more than one value at a time. 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.
- Indexed arrays – Arrays with the numeric index.
- Associative arrays – Arrays with the named keys.
- Multidimensional arrays – Arrays that are containing one or more arrays.
Now, let’s see an example where we can add the value to a PHP Array.
PHP Array Push Example
If we want to add more values to a PHP array, then 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 array_push() function.
The array_push() treats an array as a stack and pushes the passed variables onto the end of an array.
The syntax for PHP Array Push is 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 to the specified array.
The value2, value3 and so on are optional parameters. If we want to add the multiple values then and then we need to pass those parameters.
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.
<?php // app.php $netflix = ['Stranger Things', 'Black Mirror', 'Bright', 'XOXO']; $new = array_push($netflix, 'Shaft'); print_r($netflix); echo $new."\n";
So, in the above code, we have defined one array called $netflix which has four items. Now, we have added the fifth item using array_push() function in PHP and then print 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 and navigate up to the app.php file directory and type the following command to run the file.
You will see the output like below.
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, PHP Array index is starting from 0.
#Add Multiple Values To An Array
We can add multiple values to the array. See the following example.
<?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 following.
#Add Values to the Associative Array
Let’s take a scenario, where we are adding 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";
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. Let’s run the PHP file and see the output.
#Add Array in the Array
Okay, now let’s take a scenario, where we are adding 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 following.
See, it has added an array as a 3rd element, and it has its index which is 0 and 1.
So, right now, the dataA array is a multidimensional array.
#PHP Array Length
<?php $netflix = ['Stranger Things', 'Black Mirror', 'Bright', 'XOXO']; echo count($netflix);
See the following output.
➜ pro php app.php 4 ➜ 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 increasing and if use array_pop() function then it will decrease the array length by one.
Finally, PHP Array Push Example | Add Elements To An Array | PHP Array array_push() function tutorial is over.