PHP array_push lets you add one or more values to the end of an array. It appeared to make adding elements faster than writing extra lines of code.
Table of Content
Understand the array_push Function in PHP
The array_push function adds one or more values to the end of an existing array. It works with both numeric and associative arrays.
Here is the syntax:
array_push(array &$array, mixed ...$values): int
- $array is the array to which you want to add values.
- $values are the values you want to add. You can add one or more values.
Here is a quick example:
$items = ["apple", "banana"];
array_push($items, "mango", "grape");
print_r($items);
The code above adds two new values to the array in one function call:
Array
(
[0] => apple
[1] => banana
[2] => mango
[3] => grape
)
The Differences Between array_push and $array[]
The array_push
function appends one or more values to the end of an array. The $array[]
syntax is a shortcut to append a single value to the end of an array.
Here are the key differences:
Feature | array_push | $array[] syntax |
---|---|---|
Multiple values | Yes | No |
Function call needed | Yes | No |
Return value | Good for a single value | No return value |
Readability | Good for many values | Good for single value |
You can use array_push when you need to add many values in one go, and the $array[] when you want to add only one value.
Use the array_push with Associative Arrays
You can use array_push with associative arrays. It will still add values to the end, but without keys. This works well for numeric keys, but for named keys you may need direct assignment.
Here is an example:
$user = ["name" => "Sara"];
array_push($user, "Developer");
print_r($user);
This adds a new value with an automatic numeric key after the named key. The output:
Array
(
[name] => Sara
[0] => Developer
)
For another example:
$data = ["id" => 1, "role" => "Admin"];
array_push($data, "Active", "Verified");
print_r($data);
This adds two new values at the end with numeric keys. Here is the output:
Array
(
[id] => 1
[role] => Admin
[0] => Active
[1] => Verified
)
Examples of the array_push in PHP
Add values to a numeric array:
$colors = ["red", "blue"];
array_push($colors, "green", "yellow");
print_r($colors);
This code starts with two colors. It then appends two more colors to the end using array_push in one call.
Here is the output:
Array
(
[0] => red
[1] => blue
[2] => green
[3] => yellow
)
Merge user input into an array:
$input = ["PHP", "JavaScript"];
$newTech = ["Python", "Go"];
array_push($input, ...$newTech);
print_r($input);
This example takes two arrays and merges them using array_push with the spread operator. It appends all new values without a loop
The output:
Array
(
[0] => PHP
[1] => JavaScript
[2] => Python
[3] => Go
)
Use array_push with mixed data:
$data = [1, 2, 3];
array_push($data, "four", ["five", 6]);
print_r($data);
This appends a string and another array to the main array. The function handles mixed value types without issue.
The output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => four
[4] => Array
(
[0] => five
[1] => 6
)
)
Build an array dynamically:
$result = [];
array_push($result, date("Y-m-d"));
array_push($result, rand(1, 100));
print_r($result);
This builds an array step by step. It first adds the current date and then a random number using separate array_push calls.
The output:
Array
(
[0] => 2025-08-14
[1] => 80
)
Wrapping Up
In this article, you learned what array_push does and how to use it with numeric and associative arrays.
Here is a quick recap:
- It adds one or more values to the end of an array.
- You can use it with numeric and associative arrays.
- It is useful when you want to add multiple values in one call.
FAQs
How to use array_push to add elements to an array?
$fruits = ["apple", "banana"];
array_push($fruits, "orange", "grape");
print_r($fruits);
This code adds two new values at the end of the array. You can push multiple items at once in a single call.What is the difference between array_push and using $array[]?
$data = [1, 2];
$data[] = 3; // Adds one value
array_push($data, 4, 5); // Adds multiple values
print_r($data);
$array[] works for one element at a time. array_push can add one or more elements in a single call.Can you use array_push with associative arrays?
$person = ["name" => "John"];
array_push($person, "Developer");
print_r($person);
array_push works with associative arrays but adds elements with numeric keys, not custom keys.Similar Reads
PHP has a number of predefined constants that give you some information about the language and its environment. You can…
If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…
pplications. Such As as From registering new users to collecting form submissions and storing product details. Things like adding a…
Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…
The filter_input_array() filters multiple inputs in PHP at once. It helps you to clean and validate data. Understand the filter_input_array…
Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…
Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…
PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…
The PHP is_readable helps you validate file readability before any operation like read or include files. What Is PHP is_readable? The is_readable() function…
PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…