How to use array_push function in php

藏色散人
Release: 2023-02-22 18:02:02
Original
4819 people have browsed it

Usage of array_push function in php: [array_push(array,value1)]. The array_push function is used to add one or more elements to the end of the specified array and return the length of the new array.

How to use array_push function in php

php The array_push function is used to add one or more elements (push) to the end of the array of the first parameter, and then return the length of the new array. The syntax is array_push(array,value1,value2...), the parameter array is required and specifies the array; value1 is required and specifies the value to be added.

(Recommended tutorial: php video tutorial)

How to use the php array_push function?

Function: Add one or more elements (push) to the end of the array of the first parameter, and then return the length of the new array.

Syntax:

array_push(array,value1,value2...)
Copy after login

Parameters:

array Required. Specifies an array.

value1 Required. Specifies the value to add.

value2 Optional. Specifies the value to add.

Note:

Even if there are string key names in the array, the elements you add are always numeric keys. If you use array_push() to add a unit to an array, it is better to use $array[] =, because there is no additional burden of calling the function. array_push() will issue a warning if the first argument is not an array. This is different from the behavior of $var[], which creates a new array.

php array_push() function usage example 1

<?php
$a=array("西门","灭绝");
print_r(array_push($a,"无忌","黄蓉"));//返回新数组长度
echo "<br>";
print_r($a);//打印新数组
?>
Copy after login

Output:

4
Array ( [0] => 西门 [1] => 灭绝 [2] => 无忌 [3] => 黄蓉 )
Copy after login

php array_push() function usage example 2

<?php
$a=array("无忌","欧阳克");
print_r(array_push($a,"灭绝"));//返回新数组长度
echo "<br>";
print_r($a);//打印新数组
?>
Copy after login

Output:

3
Array ( [0] => 无忌 [1] => 欧阳克 [2] => 灭绝 )
Copy after login

The above is the detailed content of How to use array_push function in php. 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