PHP array_push() Function

Topics Covered

Overview

In PHP, the array_push() function is used to add one or more elements to the end of an array. It simplifies the process of appending new values to an existing array, eliminating the need to manually calculate indexes. The function takes the array as its first parameter and one or more values to be added as subsequent parameters. The new values are added to the end of the array, extending its length. array_push() is particularly useful for dynamically building arrays by gradually adding elements. However, for adding a single element, using the shorthand $array[] = $value notation is often preferred due to its brevity and efficiency.

Syntax of array_push() in PHP

The array_push() function in PHP is used to append one or more elements to the end of an array. It modifies the original array and returns the new number of elements in the array. Here's the syntax of array_push() in detail:

  • array:

    A reference to the array you want to modify by adding elements.

  • value1, value2, ...:

    The values you want to add to the end of the array. You can provide multiple values separated by commas.

Parameters of array_push() in PHP

The array_push() function in PHP is used to add one or more elements to the end of an array. It takes two or more parameters:

  • array &$array:

    This is a reference to the array you want to modify. The & symbol indicates that the array is passed by reference, meaning the modifications will be made directly to the original array. This parameter is required.

  • mixed $value1:

    This is the first value you want to add to the end of the array. It can be of any data type (e.g., string, number, array, object). This parameter is required.

  • mixed $value2, mixed $value3, ...:

    You can add more values by providing additional parameters, separated by commas. These values will be added in the order they appear. You can add as many values as needed.

Return Value of array_push() in PHP

The array_push() function in PHP modifies an array by adding one or more elements to the end of it. It doesn't return the modified array itself, but rather it returns the new number of elements in the array after the additions have been made.

Here's how the return value of array_push() works:

  • If no elements were added (i.e., you didn't provide any values to add), the function will return the original number of elements in the array.
  • If elements were added (one or more), the function will return the new total number of elements in the array after the additions.

Example:

In this example, after the array_push() calls, $result1 is 3 and $result2 is 5, reflecting the new number of elements in the modified $fruits array.

Examples of array_push() in PHP

PHP array_push Basic Program

Here's a basic PHP program that demonstrates the usage of the array_push() function to add elements to an array:

Output:

In this program:

  • We start by defining an array $fruits containing the initial fruits 'apple' and 'banana'.
  • We use array_push(\$fruits, $newFruit); to add the value 'orange' to the end of the array. This is a single-value push.
  • We use array_push(\$fruits, ...$moreFruits); to add the elements of the $moreFruits array ('grape' and 'cherry') to the end of the $fruits array. This is a multiple values push using the spread operator (...).
  • Finally, we use print_r() to display the modified array with all the added elements.

array_push function with Integer Key Value

The array_push() function in PHP works with both integer and string keys. However, it's essential to note that when you use array_push() `to add elements to an array with integer keys, the function will always append the new elements at the end of the array and reindex the keys to consecutive integers, starting from zero. Here's how it works:

Output

In this example:

  • We start with an array called $numbers containing elements with integer keys: [0 => 10, 1 => 20, 2 => 30].
  • We use array_push($numbers, 40, 50); to add the values 40 and 50 to the end of the array. As a result, the new elements are appended with integer keys [3 => 40, 4 => 50], and the keys are reindexed accordingly.
  • The print_r() function is used to display the modified array, showing the newly added elements with their respective keys.

array_push function without Integer Key-Value

Output

In this example:

  • We start with an associative array called $student, which has string keys and corresponding values: ['name' => 'John', 'age' => 25, 'major' => 'Computer Science'].
  • We use array_push($student, 'location', 'New York'); to add the elements 'location' and 'New York' to the end of the associative array. The keys of the new elements are automatically generated as integers ([0] and [1]), indicating their positions in the array.
  • The print_r() function is used to display the modified array. Note that the original keys ('name', 'age', 'major') are preserved, and the new elements are added with integer keys.

array inside an array using PHP array_push

Output

In this example:

  • We start by creating an empty parent array called $parentArray.
  • We create two child arrays, $childArray1 and $childArray2, each containing several elements.
  • We use array_push($parentArray, $childArray1); to add $childArray1 as an element to the end of $parentArray.
  • Similarly, we use array_push($parentArray, $childArray2); to add $childArray2 as another element to the end of $parentArray.
  • The print_r() function is used to display the modified parent array containing the child arrays.

An array with string keys

Here's a PHP code example that demonstrates the creation, manipulation, and access of an array with string keys (associative array), along with explanations for each step:

Output

Conclusion

  • array_push() allows for easy insertion of one or more elements at the end of an array, reducing the need for complex array manipulation code.
  • It works seamlessly with both numeric and string keys, making it suitable for both indexed and associative arrays.
  • When adding elements to an array with numeric keys, array_push() reindexes keys to consecutive integers, ensuring data integrity.
  • The function operates on the original array by reference, directly modifying it without the need for explicit assignment.
  • By simplifying the process of adding elements, array_push() improves code readability and maintainability, making codebases more comprehensible and easier to manage.
  • It's particularly useful when dynamically building arrays or managing evolving data collections, supporting the creation of flexible and adaptable applications.