Home > Backend Development > PHP Tutorial > php function array_push() that inserts one or more elements to the end of the array

php function array_push() that inserts one or more elements to the end of the array

黄舟
Release: 2023-03-17 08:14:02
Original
3960 people have browsed it

Parameter example

Insert "blue" and "yellow" to the end of array:

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
Copy after login
Copy after login

Definition and usage

array_push() FunctionInsert one or more elements to the end of the array.

Tip: You can add one or more values.

Note: Even if your array has string keys, the elements you add will be numeric keys (see example below).

Syntax

array_push(array,value1,value2...)
Copy after login
ParametersDescription
array Required. Specifies an array.
value1Required. Specifies the value to add.
value2Optional. Specifies the value to add.

Technical details

Return value: Returns the number of elements in the new array.
PHP version: 4+
##More examples

Example 1

Array with string key name:

<?php
$a=array("a"=>"red","b"=>"green");
array_push($a,"blue","yellow");
print_r($a);
?>
Copy after login

Example 1

Use the php array_push() function to insert "blue" and "yellow" at the end of the array, the code is as follows:

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
Copy after login
Copy after login
Code running result:

php function array_push() that inserts one or more elements to the end of the array


The above is the detailed content of php function array_push() that inserts one or more elements to the end of the array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template