PHP array_push Function: How it Works with Examples

php array push

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.

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:

Featurearray_push$array[] syntax
Multiple valuesYesNo
Function call neededYesNo
Return valueGood for a single valueNo return value
ReadabilityGood for many valuesGood 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 Predefined Constants Explained Simply

PHP has a number of predefined constants that give you some information about the language and its environment. You can…

PHP array_any: How it Works with Arrays with Examples

PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…

OOP Interface PHP: How to Set Rules for Classes

PHP developers relied on class inheritance to share functionality before the OOP interface, but this approach had limits. It solves…

Understanding the PHP Operator Precedence

The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…

PHP str_repeat: How it Works with Examples

PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…

PHP NOT ( ! ) Operator: A Comprehensive Guide

The NOT operator in PHP (!) provides the reverse of whatever truth value is given by a certain expression. It’s…

PHP Null: How to Assign and Check for Null Values

PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…

PHP is_dir Function: Check if a Directory Exists

Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…

PHP strtoupper Function: Convert Strings to Uppercase

Use strtoupper() function when you want to change all letters in a string to uppercase in PHP. It works with…

PHP OR Operator: Using || and or in Conditional Statements

The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…

Previous Article

PHP array_filter: How to Filter Array Values with Examples

Next Article

HTML s Tag: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.