PHP array_push Function
last modified March 13, 2025
The PHP array_push
function adds one or more elements to the end
of an array. It's a convenient way to append values to an existing array.
Basic Definition
The array_push
function pushes elements onto the end of an array.
It modifies the original array and returns the new number of elements.
Syntax: array_push(array &$array, mixed ...$values): int
. The
first parameter is the array to modify, followed by values to add.
Basic array_push Example
This demonstrates adding a single element to an array using array_push.
<?php $fruits = ["apple", "banana"]; $count = array_push($fruits, "orange"); print_r($fruits); // Output: Array ( [0] => apple [1] => banana [2] => orange ) echo "New count: $count"; // Output: New count: 3
This adds "orange" to the $fruits array. The function returns 3, the new count of elements. The original array is modified.
Adding Multiple Elements
array_push can add multiple elements at once to an array.
<?php $numbers = [1, 2]; $newCount = array_push($numbers, 3, 4, 5); print_r($numbers); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) echo "New count: $newCount"; // Output: New count: 5
This adds three numbers (3, 4, 5) to the $numbers array. The function returns the new total count of 5 elements in the array.
Using array_push with Associative Arrays
array_push works differently with associative arrays compared to indexed ones.
<?php $user = ["name" => "John", "age" => 30]; $count = array_push($user, "New York"); print_r($user); // Output: Array ( [name] => John [age] => 30 [0] => New York )
With associative arrays, array_push adds elements with numeric keys. The "New York" value gets index 0 since it's the first numerically indexed element.
Alternative Syntax with []
The [] syntax is often simpler than array_push for adding single elements.
<?php $colors = ["red", "green"]; $colors[] = "blue"; // Equivalent to array_push($colors, "blue") print_r($colors); // Output: Array ( [0] => red [1] => green [2] => blue )
The [] syntax is cleaner for single elements but array_push is better for multiple elements. Both modify the original array.
Performance Considerations
array_push has different performance characteristics than the [] syntax.
<?php $largeArray = range(1, 100000); // Test array_push $start = microtime(true); array_push($largeArray, 100001); $timePush = microtime(true) - $start; // Test [] syntax $start = microtime(true); $largeArray[] = 100001; $timeBracket = microtime(true) - $start; echo "array_push: " . $timePush . " seconds\n"; echo "[] syntax: " . $timeBracket . " seconds\n";
For single elements, [] is generally faster than array_push. However, for multiple elements, array_push can be more efficient than multiple [] operations.
Best Practices
- Multiple Elements: Use array_push for adding several values at once.
- Single Element: Prefer [] syntax for cleaner code.
- Return Value: Utilize the count return when needed.
- Associative Arrays: Be aware of numeric key behavior.
Source
This tutorial covered the PHP array_push
function with practical
examples showing its usage for adding elements to arrays.
Author
List all PHP Array Functions.