PHP: array_push() function
PHP: Push one or more elements onto the end of array
The array_push() function is used to add one or more elements onto the end of an array. The length of array increases by the number of variables pushed.
Version:
(PHP 4 and above)
Syntax:
array_push(array_name, value1, value2...)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array_name | The input array | Required | Array |
value1 | Value to add. | Required | Mixed* |
value2.. | Value to add. | Optional | Mixed* |
*Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Return value:
The new number of elements inserted in array_name.
Value Type: Integer.
Example:
<?php
$array1= array('Mathematics','Physics');
array_push($array1,'Chemistry','Biology');
print_r($array1);
?>
Output:
Array ([0] => Mathematics [1] => Physics [2] => Chemistry [3] => Biology)
Pictorial Presentation:

View the example in the browser
Practice here online :
See also
Previous: array_pop
Next: array_rand
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises