The PHP array_push() function adds one or more values to the end of an array. It is useful when you already have an array and want to append new items later in the code.
For adding a single value, PHP also gives a simpler option: the square bracket syntax $array[] = $value. For adding multiple values at once, array_push() can make the code clear.
This tutorial is a focused supporting guide for the main PHP arrays tutorial. Here, we will go deeper into array_push(), adding single and multiple values, using it in loops, avoiding duplicates, and choosing between array_push(), [], and array_merge().
Quick Answer
Use array_push() to append values to the end of an existing array.
<?php
$fruits = ["Apple", "Orange"];
array_push($fruits, "Mango", "Grapes");
print_r($fruits);
?>
Output:
Array
(
[0] => Apple
[1] => Orange
[2] => Mango
[3] => Grapes
)
The new values are added to the end of the array in the same order.
Syntax of array_push()
array_push(array &$array, mixed ...$values): int
- $array – The input array. New values are added to this array.
- $values – One or more values to add.
The function returns the new number of elements in the array after the values are added.
<?php
$items = ["Laptop", "Mouse"];
$totalItems = array_push($items, "Keyboard");
echo $totalItems;
?>
Output:
3
Add a Single Value to an Array
You can use array_push() to add one value to an array.
<?php
$numbers = [10, 20, 30];
array_push($numbers, 40);
print_r($numbers);
?>
Output:
Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
)
For a single value, the square bracket syntax is usually simpler.
<?php
$numbers = [10, 20, 30];
$numbers[] = 40;
print_r($numbers);
?>
Both examples give the same result. In daily PHP code, many developers prefer $array[] = $value for appending one item.
Add Multiple Values to an Array
The array_push() function is more useful when you want to add more than one value at the same time.
<?php
$skills = ["PHP", "MySQL"];
array_push($skills, "JavaScript", "React", "Laravel");
print_r($skills);
?>
Output:
Array
(
[0] => PHP
[1] => MySQL
[2] => JavaScript
[3] => React
[4] => Laravel
)
This is cleaner than writing separate append statements for each value.
<?php
$skills[] = "JavaScript";
$skills[] = "React";
$skills[] = "Laravel";
?>
Use array_push() in a Loop
You can also use array_push() inside a loop to build a new array from another list.
<?php
$products = [
["name" => "Keyboard", "price" => 1200],
["name" => "Mouse", "price" => 600],
["name" => "Monitor", "price" => 9500]
];
$affordableProducts = [];
foreach ($products as $product) {
if ($product["price"] < 2000) {
array_push($affordableProducts, $product["name"]);
}
}
print_r($affordableProducts);
?>
Output:
Array
(
[0] => Keyboard
[1] => Mouse
)
This example creates a new array with only the product names that match the condition.
Avoid Duplicate Values Before Pushing
Sometimes, you may want to add a value only if it is not already present in the array.
For this, check the value first with in_array(). Then push it only when it does not exist.
<?php
$selectedTags = ["php", "mysql"];
$newTag = "php";
if (!in_array($newTag, $selectedTags, true)) {
array_push($selectedTags, $newTag);
}
print_r($selectedTags);
?>
Output:
Array
(
[0] => php
[1] => mysql
)
The value "php" was not added again because it already exists in the array.
If you want to learn this check in detail, read the PHP in_array tutorial.
Push an Array as a Single Item
If you pass another array to array_push(), PHP adds it as one nested item. It does not merge the values automatically.
<?php
$list = ["A", "B"];
$newItems = ["C", "D"];
array_push($list, $newItems);
print_r($list);
?>
Output:
Array
(
[0] => A
[1] => B
[2] => Array
(
[0] => C
[1] => D
)
)
This is correct if you want a nested array. If you want to combine two arrays into one flat array, use array_merge() instead.
array_push() vs array_merge()
Use array_push() when you want to append one or more individual values to an existing array.
Use array_merge() when you want to combine two arrays into one flat array.
<?php
$first = ["A", "B"];
$second = ["C", "D"];
$merged = array_merge($first, $second);
print_r($merged);
?>
Output:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
The official PHP manual for array_push() also notes that if you use array_push() to add only one element, using $array[] = $value is usually simpler.
array_push() with Associative Arrays
array_push() adds values with numeric keys. It is not the right choice when you want to add a named key to an associative array.
<?php
$user = [
"name" => "Vincy",
"role" => "editor"
];
array_push($user, "active");
print_r($user);
?>
Output:
Array
(
[name] => Vincy
[role] => editor
[0] => active
)
If you want to add a named key, assign it directly.
<?php
$user["status"] = "active";
print_r($user);
?>
Output:
Array
(
[name] => Vincy
[role] => editor
[status] => active
)
Common Mistakes and Fixes
1. Using array_push() for one value every time
This is not wrong, but it is usually longer than needed.
<?php
$items = ["Pen", "Pencil"];
array_push($items, "Eraser");
?>
Fix: For one value, prefer this simpler syntax.
<?php
$items[] = "Eraser";
?>
2. Expecting array_push() to preserve custom keys
array_push() appends values with numeric keys. It does not add named keys to an associative array.
Fix: Use direct key assignment for associative arrays.
<?php
$user["status"] = "active";
?>
3. Passing an array and expecting a flat result
If you push an array, it becomes a nested array item.
Fix: Use array_merge() when you want to combine arrays into a flat array.
Security Considerations
array_push() only adds values to an array. It does not validate, sanitize, or escape data.
- Validate user input before pushing it into an array.
- Use
in_array()with strict mode when checking against allowed values. - Escape values with
htmlspecialchars()before showing them in HTML. - Use prepared statements if the pushed values are later used in database queries.
For example, if you collect selected tags from a form, check each tag against an allowed list before adding it to the final array.
Complete Example: Add Tags Without Duplicates
This example shows a simple form where a user can add tags. The code ensures that duplicate tags are not added.
<?php
$allowedTags = ["php", "mysql", "javascript", "react"];
$selectedTags = [];
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$inputTag = $_POST["tag"] ?? "";
if ($inputTag !== "" && in_array($inputTag, $allowedTags, true)) {
if (!in_array($inputTag, $selectedTags, true)) {
array_push($selectedTags, $inputTag);
}
}
}
?>
<form method="post">
<label for="tag">Add tag</label>
<input type="text" name="tag" id="tag">
<button type="submit">Add</button>
</form>
<?php if (!empty($selectedTags)): ?>
<h3>Selected Tags</h3>
<ul>
<?php foreach ($selectedTags as $tag): ?>
<li><?php echo htmlspecialchars($tag); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This example combines two ideas:
- Validate input using
in_array() - Add values using
array_push()

Demo output showing PHP array_push adding tags without duplicates.
Screenshot to capture: Run the demo project. Add a valid tag like php and capture the list output. Then try adding the same tag again and show that it is not duplicated. One screenshot is enough.
Developer FAQ
Is array_push() faster than using []?
No. For adding a single value, $array[] = $value is slightly faster and simpler.
Can I use array_push() with associative arrays?
It works, but it adds values with numeric keys. Use direct assignment if you need named keys.
Can I push multiple values at once?
Yes. You can pass multiple values as arguments to array_push().
Does array_push() modify the original array?
Yes. It modifies the array directly and returns the new length.
Conclusion
The array_push() function is a simple way to add values to the end of an array. It is useful when adding multiple values or when you want clear intent in your code.
For single values, use $array[] = $value. For merging arrays, use array_merge(). For avoiding duplicates, combine it with in_array().
Download Source Code
Download the complete PHP array_push() demo project and run it locally.
Download the PHP array_push demo project
The project includes a simple tag input form, clean PHP code, and setup instructions. No database is required.