array_push
Appends elements to the end of an array.
Syntax
array_push(array &$array, mixed ...$values): intParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The array being modified. |
| ...$values | mixed | The values to append. |
Return value
intThe new number of elements.
Examples
Several values at once:
<?php
$colors = ['red'];
array_push($colors, 'green', 'blue');
echo implode(', ', $colors);Output:
red, green, blueFor a single value the bracket form is simpler:
<?php
$colors = ['red'];
$colors[] = 'green';
echo count($colors);Output:
2Notes
The function modifies the array itself. $array[] = $value does the same and is faster.
See also
- array_pop — Removes the last element of an array and returns it.
- array_unshift — Prepends elements to the beginning of an array.
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.