Examples of PHP array_push()
Basic Example
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
$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
$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
$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)
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
Check out the following problems -
Refer to our guided paths on Code360 to learn more! 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.