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 OOP Constructor: How It Works in a Class with Examples

The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

Parameters and Arguments in PHP: What’s the Difference?

If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

PHP array_key_exists: How it Works with Examples

The PHP array_key_exists function checks a given key in an array and tells if that key exists or not. Understand…

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…

How to Connect PHP to MongoDB

Connecting PHP to MongoDB allows you to integrate database functionalities into your web applications. MongoDB is a NoSQL database that…

PHP array_merge_recursive: Merge Arrays Deeply

PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…

How to Create Table in MySQL with PHP?

One of the first tasks you will likely encounter is creating a table in databases. Tables are the main part…

PHP function_exists: Avoid Function Redeclaration

The function_exists() function in PHP checks whether a given function is defined or not. The function_exists() function works for both…

PHP Type Hints: Ensuring Data Integrity

PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…

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.