PHP Array Push | Overview of PHP array_push() Function With Example
Summary: In this article, you will learn the PHP array push function, and How to add elements to the end of an array in PHP with examples. Let’s understand in detail.
Definition and Usages
- The array_push() function in PHP is basically used to insert the new elements into the end of an array and also get the updated number of array elements. You may add as many values as your requirement.
- The elements that you added will always have numeric keys, even if the array itself has string keys. Furthermore, the PHP array push function has been proposed in PHP 4.
- You can say another word that its function is used to add one or more elements onto the end of an array. The length of the array will increase by the number of variables pushed.
- This PHP function is an inbuilt function, An array can store multiple values in one single variable. PHP add to array is an operation in which we append the elements to the existing array. and you are able to access the values by referring to an index number.
Syntax:
Parameters Value:
Name | Description | Type |
---|---|---|
array_name | It is required, the input array | Array |
value1 | It is required, value to add | Mixed* |
value2 | It is optional, value to add | Mixed* |
value3.. | It is optional, value to add | Mixed* |
PHP Array Push Example
Program:1
The given the following example. you can see an array that contains string keys. furthermore, let’s see how two new values are added at the end of it using the PHP array_push function:
1 2 3 4 5 6 7 8 |
<?php $arr = [ 'flower' => 'Red', 'veg' => 'green' ]; array_push($arr, 'brown', 'pink'); print_r($arr); ?> |
Program:2
Here is a simple example, how this function is used to insert elements into the end of an array.
1 2 3 4 5 |
<?php $y = ['he','me', 'you']; array_push($y, 'him', 'it'); print_r($y); ?> |
Program:3
1 2 3 4 5 |
<?php $array1= array('Lucknow','Delhi'); array_push($array1,'Bhopal','Nagpur'); print_r($array1); ?> |
How to Add multiple values to PHP array
PHP array_push() function takes multiple elements and appends all the elements into the array. It’ll add in the order that they’re added. It doesn’t change its order.
1 2 3 4 5 6 |
<?php $Flowers = ['Rose', 'Lotus', 'Butterfly Pea', 'Crossandra']; $new = array_push($Flowers, 'Golden Shower Flower', 'Yellow Marigold', 'Pot Marigold', 'Jasmine', 'Star Jasmine'); print_r($Flowers); echo $new."\n"; ?> |
How to Add values to the Associative Array
Let’s take a scenario where we are adding values to the Associative Array.
1 2 3 4 5 6 |
<?php $studentdata = ['name' => 'Saumya', 'education' => 'LLB']; $new = array_push($studentdata, 'Ritu', 'MBA'); print_r($studentdata); echo $new."\n"; ?> |
Therefore, in the above program, the $studentdata variable is Associative Array, and we have added two values to that array. means that the first two items are associative, which have their key. But, from 3rd & 4th, they have indexes starting from 0.
How to Adding array into an array in PHP
now let’s take a scenario of program where we add a whole array inside an array and get the output.
1 2 3 4 5 6 7 8 |
<?php $studentdata = ['name' => 'Krunal', 'education' => 'BE']; $second = ['Whatsapp', 'Youtube']; $newA = array_push($studentdata, $second); print_r($studentdata); echo $newA."\n"; ?> |
See, as per result, it has added an array as a third element, and it has its index, which is 0 and 1. therefore, right now, the studentdata
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 or find the next key. thus we can use the PHP array_push() method, but adding the index starts from 0 (zero) and 1 and not the keys we desire. therefore, if you want to push key and value, then you can do the following program code.
1 2 3 4 5 6 |
<?php $studentdata = ['name' => 'Saumya', 'education' => 'LLB']; $studentdata['age'] = 29; $studentdata['business'] = 'Law'; print_r($studentdata); ?> |
[name] => Saumya
[education] => LLB
[age] => 29
[business] => Law
)
as per output, you can see that we can add multiple keys of your choice and not the ones that numeric keys PHP provides by default. Furthermore, pushing a value into the array automatically generates or creates a numeric key for it.
Furthermore, When inserting a key-value pair to the array, you already have the key, and you do not require one to be created for you. That key is the numeric key, which starts from 0 (zero).
How to Add 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. therefore, it appends the item at the beginning or starting of the array at the index of 0 (zero).
1 2 3 4 5 |
<?php $course = ['HTML', 'Python', 'SQL Server', 'Javascript', 'jquery']; array_unshift($course, 'SQL'); print_r($course); ?> |
[0] => SQL
[1] => HTML
[2] => Python
[3] => SQL Server
[4] => Javascript
[5] => jquery
)
as per the result, you can see that the new element “SQL” is added at the index position 0.
The PHP array_unshift() function adds new elements to the array. The new array values will be inserted at the beginning or start of the array. thus you can insert one value or as many as you like. furthermore, the numeric keys will start at 0 and increase by 1 every time a new element is added. String keys will remain the same.
How to measuring PHP array length
The total number of items in the array can be measure or calculated by a couple of PHP functions such as count() and sizeof(), furthermore, whereas a sizeof() function is an alias of the master function PHP count().
1 2 3 4 |
<?php $grocery = ['Turmeric powder', 'Basmati rice', 'Besan flour', 'vermicelli', 'Tamarind', 'Wheat rava']; echo count($grocery); ?> |
as per results, you can see that we have six elements in the $grocery
array. Furthermore, if we add more items inside the array, then the size of the array increasing and if we use the PHP array_pop() function, then it will decrease the array length by one.
Conclusion:
In this article, you have learned How use PHP Array Push and overview of PHP array_push() Function with Example. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!