Home  >  Article  >  Backend Development  >  PHP array function sequence array_push() adds one or more elements (push) to the end of the array and returns the new length. _PHP Tutorial

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

WBOY
WBOYOriginal
2016-07-21 15:22:381084browse

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 then 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

Copy code The code is as follows:

$a= array("Dog","Cat");
array_push($a,"Horse","Bird");
print_r($a);
?>

Output:

Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird ) Example 2
with string Array of keys:
Copy code The code is as follows:

$a=array("a "=>"Dog","b"=>"Cat");
array_push($a,"Horse","Bird");
print_r($a);
?> ;

Output:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324618.htmlTechArticlearray_push() Definition and usage array_push() function adds one or more elements to the end of the array of the first parameter (pushed onto the stack), then returns the length of the new array. This function is equal to multiple calls...
Statement:
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