array_push() Function In PHP: Syntax And Examples
array_push() Function In PHP: Syntax And Examples

array_push() Function In PHP: Syntax And Examples

Introduction

PHP array_push() function is a handy tool for adding one or more elements to the end of an array. This function appends elements to the end of the array and returns the count of elements in the array after the addition. It can be used to create a dynamic array, add new values to an array, or merge arrays together.

Syntax

The syntax of the array_push() function in PHP is as follows:

array_push($array, $value1, $value2, ...);

Here:

  • $array: is the array to which elements need to be added. This parameter is passed by reference, meaning the array will be modified after the function call.
  • $value1, $value2, ...: are the elements to be added to the array. These elements can be of any data type, including arrays.

Function Parameters

The array_push() function in PHP requires at least one parameter, which is the array to which elements will be added. If there are no additional parameters, the function does nothing and returns the count of elements in the original array. If there are more than one parameter, the function adds the subsequent parameters to the end of the array, with integer keys starting from the largest key in the original array. If the original array has string keys, the added elements won't affect them.

Return Value

The array_push() function in PHP returns the count of elements in the array after the addition. If the array is not a variable or cannot be modified, the function returns NULL and raises an error.

Examples

Example 1: Adding a single element to an array

$fruits = array("apple", "banana", "orange");
array_push($fruits, "mango");
print_r($fruits);

Result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => mango
)

Example 2: Adding multiple elements to an array

$numbers = array(1, 2, 3);
array_push($numbers, 4, 5, 6);
print_r($numbers);

Result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Example 3: Adding an array to another array

$colors = array("red", "green", "blue");
$more_colors = array("yellow", "pink", "purple");
array_push($colors, ...$more_colors);
print_r($colors);

Result:

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => Array
        (
            [0] => yellow
            [1] => pink
            [2] => purple
        )

)

Some Notes on Using array_push()

  • The array_push() function in PHP can only add elements to the end of the array, not to the beginning or middle. If you want to add elements to the beginning, you can use the array_unshift() function. For adding elements in the middle, you can use the array_splice() function.
  • The array_push() function in PHP has lower performance compared to using the [] operator to add elements to an array. Therefore, if you only need to add a single element to an array, it's advisable to use the following approach:
$array[] = $value;
  • The array_push() function in PHP can add elements of different data types to an array, including arrays. However, if you add an array to another array, the added array will be treated as a single element, not a nested array. If you want to combine two arrays into a single array, you can use the array_merge() function.

Related Functions to array_push()

  • array_pop(): This function is used to retrieve and remove the last element of an array.
  • array_shift(): This function is used to retrieve and remove the first element of an array.
  • array_unshift(): This function is used to add one or more elements to the beginning of an array.
  • array_splice(): This function is used to add, remove, or replace one or more elements at any position in an array.
  • array_merge(): This function is used to combine two or more arrays into a single array.

Conclusion

The array_push() function in PHP is a useful tool for adding one or more elements to the end of an array. It has a simple syntax, is easy to use, and can add elements of different data types to an array. However, it has some performance and flexibility drawbacks, so it's essential to choose the appropriate method for adding elements to an array based on specific use cases.