PHP array function sequence array_push adds one or more elements (push) to the end of the array and returns the new length.

PHP中文网
Release: 2023-02-28 21:32:01
Original
1449 people have browsed it

array_push() Definition and Usage The
array_push() function adds one or more elements (push) to the end of the array of the first parameter and returns the length of the new array.
This function is equivalent to calling $array[] = $value multiple times.
Syntax
array_push(array,value1,value2...) Parameter Description
array Required. Specifies an array.
value1 required. Specifies the value to add.
value2 optional. Specifies the value to add.
Tips and Notes
Note: Even if there are string key names in the array, the elements you add are always numeric keys. (See Example 2)
Note: If you use array_push() to add a unit to the array, it is better to use $array[] =, because there is no additional burden of calling the function.
Note: 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.
Example 1

The code is as follows:

<?php 
$a=array("Dog","Cat"); 
array_push($a,"Horse","Bird"); 
print_r($a); 
?>
Copy after login

Output:
Array ([0] => Dog [1] => Cat [2] => Horse [3] => Bird )

Example 2
Array with string keys:

Code is as follows:

<?php 
$a=array("a"=>"Dog","b"=>"Cat"); 
array_push($a,"Horse","Bird"); 
print_r($a); 
?>
Copy after login

Output:
Array ( [a] => Dog [b] => Cat [0] => Horse [1] => Bird )

The above introduces the array_push of the PHP array function sequence, which adds one or more elements (push) to the end of the array and returns the new length. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!