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:

array_push(array_name, value1, value2, value3…)

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*
.
Important Note: Here, mixed indicates that a parameter may accept multiple types but not necessarily all.

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:

Array ( [flower] => Red [veg] => green [0] => brown [1] => pink )

Program:2

Here is a simple example, how this function is used to insert elements into the end of an array.

Array ( [0] => he [1] => me [2] => you [3] => him [4] => it )

Program:3

Array ( [0] => Lucknow [1] => Delhi [2] => Bhopal [3] => Nagpur )

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.

Array ( [0] => Rose [1] => Lotus [2] => Butterfly Pea [3] => Crossandra [4] => Golden Shower Flower [5] => Yellow Marigold [6] => Pot Marigold [7] => Jasmine [8] => Star Jasmine ) 9

How to Add values to the Associative Array

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

Array ( [name] => Saumya [education] => LLB [0] => Ritu [1] => MBA ) 4

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.

Array ( [name] => Krunal [education] => BE [0] => Array ( [0] => Whatsapp [1] => Youtube ) ) 3

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.

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

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

Array Length: 6

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!