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 Loop: How Loops Work with Examples

A loop in PHP helps you run tasks without repeating the same lines. In this article, you will learn how…

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 continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

PHP Magic Constants: Simplify Your Code

There are some tools known as "PHP magic constants" that will make your code a little more clever and versatile.…

Traits in PHP: What They Are & Examples

PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP List MongoDB Collections

Sometimes, you may need to list collections with MongoDB in a PHP environment to manage or analyze your database structure.…

PHP filter_id Function: How to Retrieve PHP Filter IDs

Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP…

PHP AND (&&) Operator: Usage & Examples

As you are entering into PHP, it would be essential to understand how logical operators work. The PHP AND operator…

PHP Array: Accessing and Managing Elements

PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

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.