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.
array_push(array_name, value1, value2, value3…)
It is required, the input array
It is required, value to add
It is optional, value to add
It is optional, value to add
Important Note: Here, mixed indicates that a parameter may accept multiple types but not necessarily all.
PHP Array Push Example
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:
Array ( [flower] => Red [veg] => green  => brown  => pink )
Here is a simple example, how this function is used to insert elements into the end of an array.
Array (  => he  => me  => you  => him  => it )
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.
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.
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).
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().
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.
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 email@example.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!