What is the PHP array_push() function?

The PHP array_push() function inserts one or more elements to the end of an array.

svg viewer

Syntax

The function is written as follows:

array_push(arr, value1, value2, ...)

Code

The following example uses the array_push() function to add elements to an array:

<?php
$arr = array("dog", "snake");
array_push($arr, "dove", "panda", "hippo");
print_r($arr);
?>

According to PHP’s official documentation, array_push() is optimal when used for inserting multiple elements into an array at once; however, it is not optimal for single insertions.

Copyright ©2024 Educative, Inc. All rights reserved