Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
What is array_push() in PHP?
1.1.
Syntax of PHP array_push() function
1.2.
Parameters of array_push() in php
1.3.
Return Value of array_push() in php
2.
Examples of PHP array_push()
2.1.
Basic Example
2.2.
PHP
2.3.
array_push() function in PHP with Integer key Value
2.4.
PHP
2.5.
array_push() function in PHP without Integer key Value
2.6.
PHP
2.7.
Push an array inside an array using the array push() function in PHP
2.8.
PHP
3.
Frequently Asked Questions
3.1.
How to push to an array in PHP
3.2.
How to push in an existing array in PHP?
3.3.
What is the difference between array_push and Array_merge?
3.4.
What is the difference between array push and pop in PHP?
3.5.
How to push an empty array in PHP?
4.
Conclusion
Last Updated: Aug 20, 2024
Easy

PHP array_push() Function

Author yuvatimankar
0 upvote

 

In PHP, methods are essential functions within classes or objects, facilitating object-oriented programming, code organization, abstraction, and encapsulation. Understanding methods is crucial for implementing inheritance, polymorphism, and building modular, reusable code. The array_push() is a vital function for convenient and readable array manipulation, widely used in dynamic PHP applications.

array_push() in php

In this blog, we will discuss the array_push() in php. Also, we will give you an easy understanding of what the array push() function in PHP is and how it functions.

What is array_push() in PHP?

The array_push() function in php adds new elements to the Array's end and modifies the Array's element count. You are allowed to add as many elements as you want. If the Array has string keys, added elements will still have numeric keys. Because of the addition of new elements, the length of an array will increase by the number of elements pushed. 

Syntax of PHP array_push() function

array_push($array, $elm1, $elm2, $elm3....)
You can also try this code with Online PHP Compiler
Run Code

Parameters of array_push() in php

Depending on the elements added, the function would take different parameters. Parameters are divided into two parts:

Parameters

ParameterTypeDescription
$arrayArrayThis applies to the Array to which we want to add elements.
List of elements $elmInteger, Float, String, ArrayElements that we want to insert into the initial Array

Return Value of array_push() in php

array_push() function in PHP returns an updated array with new elements pushed into it on end. If the initial array consists of an integer key value, then the function will always add a numeric key to the pushed value.

Examples of PHP array_push()

Basic Example

  • PHP

PHP

<?php
$classTenthSubjects = array('Hindi','Maths','Chemistry','English');
echo "Array before push operation: ";
print_r($classTenthSubjects);
array_push($classTenthSubjects,"Social Science","Yoga");
echo "<br><br>";
echo "Array after push operation: ";
print_r($classTenthSubjects);
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Array before push operation: Array([0]=>Hindi[1]=>Maths[2]=>Chemistry[3]=>English)
Array after push operation: Array([0]=>Hindi[1]=>Maths[2]=>Chemistry[3]=>English[4]=>Social Science[5]=>Yoga)

array_push() function in PHP with Integer key Value

  • PHP

PHP

<?php
$Subjects = array(1=>'Hindi',2=>'Physics',3=>'Chemistry',4=>'English');
echo "Array before push operation: ";
print_r($Subjects);
array_push($Subjects,"Dance","Yoga");
echo "<br><br>";
echo "Array after push operation: ";
print_r($Subjects);
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Array before push operation: Array([1]=>Hindi[2]=>Physics[3]=>Chemistry[4]=>English)
Array after push operation: Array([1]=>Hindi[2]=>Physics[3]=>Chemistry[4]=>English[5]=>Dance[6]=>Yoga)

array_push() function in PHP without Integer key Value

  • PHP

PHP

<?php
$Countries = array(a=>'India',b=>'USA',c=>'China',d=>'England');
echo "Array before push operation: ";
print_r($Countries);
array_push($Countries,"Afghanistan","Bangladesh");
echo "<br><br>";
echo "Array after push operation: ";
print_r($Countries);
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Array before push operation: Array([a]=>India[b]=>USA[c]=>China[d]=>England)
Array after push operation: Array([a]=>India[b]=>USA[c]=>China[d]=>England[0]=>Afghanistan[1]=>Bangladesh)

Push an array inside an array using the array push() function in PHP

  • PHP

PHP

<?php
$Countries = array('India','China','USA','Pakistan');
echo "Array before push operation: ";
print_r($Countries);
array_push($Countries,array('Indonesia', 'Canada','Afganistan','Dubai'));
echo "<br><br>";
echo "Array after push operation: ";
print_r($Countries);

?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Array before push operation: Array([0]=>India[1]=>China[2]=>USA[3]=>Pakistan)
Array after push operation: Array([0]=>India[1]=>China[2]=>USA[3]=>Pakistan[4]=>Array([0]=>Indonesia[1]=>Canada[2]=>Afghanistan[3]=>Dubai)

 

Must Read: Laravel Facades

Frequently Asked Questions

How to push to an array in PHP

In PHP, to add elements to an existing array, you can either use the `array_push()` function or simply append items using the `[]` syntax. For example, using `array_push($array, 'value')` adds 'value' to the end of `$array`. Alternatively, `$array[] = 'value'` achieves the same result by directly appending 'value' to the array. Both methods are commonly used for adding items dynamically to an array in PHP.

How to push in an existing array in PHP?

To push elements into an existing array in PHP, use array_push($array, $element1, $element2, ...), adding desired elements to the array.

What is the difference between array_push and Array_merge?

array_push appends elements to the end of an array, modifying it. array_merge creates a new array by combining multiple arrays without modifying the originals.

What is the difference between array push and pop in PHP?

In PHP, `array_push()` adds one or more elements to the end of an array, while `array_pop()` removes the last element from an array and returns that element. These functions modify the original array.

How to push an empty array in PHP?

To push an empty array in PHP, use array_push($array, array()) or simply $array[] = array(). This adds an empty array as an element to the existing array.

Conclusion

In this article, we have discussed the PHP array_push() Function. We have seen its syntax, parameters, and return values. Then we have explained this method with the help of different examples.

Recommended Articles PHP Introduction5 Best Free PHP Projects with Source CodeFunctions in PHPForm Handling in PHPPHP file handlingIntroduction to OOP in PHPBest PHP certifications, PHP or Node.js.

Check out the following problems - 


Refer to our guided paths on Code360 to learn more about DSA, Competitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Live masterclass