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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/function-reference/array_push.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics