HOWTO · PHP

How to Push Items to Associative Array in PHP

In this tutorial, we will see how you can add items or elements into an associative array in PHP.

On this page

To add an item to a PHP associative array, choose the operation based on the key you want: array_push() appends values with numeric keys, while array_merge() can add an entry under a named string key. If the arrays contain the same string key, array_merge() keeps the later value; array_merge_recursive() instead groups colliding values into an array.
The code examples have been executed successfully, and their results match the behavior described below.

What Is an Associative Array in PHP?

PHP arrays are ordered maps: each value is stored under an integer or string key. This means an array can act like a list, a map of names to values, or a combination of both. A string-keyed array is commonly called an associative array. The keys let you retrieve values by meaning rather than by position, as in $age['Mike'] below.

Run this example:

<?php
$age = ['Mike' => 24, 'Ann' => 19, 'Alice' => 32];
echo "Mike is {$age['Mike']} years old.";

This structure is useful when each value has a stable label, such as a person’s name, a product SKU, or a configuration option. Use the same key spelling when reading an entry; keys are case-sensitive, so 'Mike' and 'mike' identify different entries. An array can contain both string and integer keys, which is why the operation used to add a value matters.

Append Values with array_push()

array_push() is a function that appends one or more values to the end of an array. PHP assigns those appended values integer keys; the function does not let you choose a string key such as 'c'. Use it when the new values belong in a numeric sequence, even if the array already contains named entries. If you need a particular name, use the next section’s merge pattern instead.

<?php
$color = ['a' => 'Red', 'b' => 'Blue'];
array_push($color, 'Green', 'White');
print_r($color);

The two new values are appended in order and receive integer keys, while the existing string-keyed entries remain. For a single value, $color[] = 'Green'; is a concise alternative with the same append behavior. Neither form creates a new named entry. When the integer keys matter to later code, inspect the resulting keys rather than assuming that the array is a zero-based list.

Add Named Entries with array_merge()

To add a named entry, create a small associative array containing that key and value, then merge it with the original. The returned array contains the entries from both inputs; assigning it back updates the variable, while using it directly leaves the original variable unchanged.

<?php
$age = ['Mike' => 24, 'Ann' => 19, 'Alice' => 19];
$age = array_merge($age, ['John' => 22]);
print_r($age);

Here, 'John' remains a string key, so it can be accessed as $age['John']. You can pass more than two arrays, and their order controls the result. For string keys that occur more than once, the value from the later array replaces the earlier value. For example, merging ['b' => 'Blue'] with ['b' => 'Neon'] leaves 'b' => 'Neon'. This replacement behavior is useful when later settings should override defaults, but it discards the earlier value for that key.

There is a separate rule for integer keys: array_merge() renumbers numeric keys in the result, starting at zero, in encounter order. Therefore, do not use it when the original integer keys must be preserved. For a simple named addition, the + array union operator is another option, but it preserves keys and keeps the left-hand value on a duplicate key. Choose it only when that precedence is what you want.

Handle Duplicate String Keys with array_merge_recursive()

When duplicate string-key values should both remain available, array_merge_recursive() combines them under that key in an array. This differs from array_merge(), which replaces the earlier value. It also changes the value’s shape: code that previously expected a string at $color['b'] must now handle an array there. Use this function when grouped values are appropriate, not as a drop-in replacement for ordinary merging.

<?php
$color = ['a' => 'Red', 'b' => 'Blue'];
$otherColor = ['b' => 'Neon', 'c' => 'Green'];
$result = array_merge_recursive($color, $otherColor);
print_r($result);

The result keeps 'a' and 'c' as single values, while 'b' contains both 'Blue' and 'Neon' in an indexed array. If a duplicated key already holds an array, recursive merging can combine nested values as well, so inspect the resulting structure before treating it as a scalar. In short, use array_push() for appended numeric-keyed values, array_merge() for named entries with later-value replacement, and array_merge_recursive() only when combined duplicate values and the resulting nested structure are intended.