📜  php array_push - PHP (1)

📅  最后修改于: 2023-12-03 14:45:10.426000             🧑  作者: Mango

PHP array_push

Introduction

PHP array_push function is used to insert one or more elements into the end of an array. It returns the new number of elements in the array after the insertion.

Syntax

The syntax of the array_push function is:

array_push ( array &$array , mixed $value1 [, mixed $... ] ) : int

The array_push function expects at least two parameters:

  • $array: The array to which the values will be added.
  • $value1: First value to push onto the end of the array.

If you have more than one value to add, you can add them as arguments separated by commas.

Return Value

The array_push function returns the new number of elements in the array after the insertion.

Examples
<?php
$fruits = array("apple", "banana");

// add one value
array_push($fruits, "orange");
// $fruits is now array("apple", "banana", "orange")

// add more than one value
array_push($fruits, "lemon", "pear");
// $fruits is now array("apple", "banana", "orange", "lemon", "pear")
?>
Conclusion

In this tutorial, you have learned what is PHP array_push function, its syntax, return value, and examples of how to use it.

array_push is a useful function when you need to add one or more elements into an existing array.