Home > Article > Backend Development > php array_push() function
The array_push() function in php adds one or more elements (to the stack) to the end of the array of the first parameter, and then returns the length of the new array. This article introduces the syntax of the php array_push() function and some small examples. Let’s take a look.
#php array_push() function?
The array_push() function in php adds one or more elements (to the stack) to the end of the array of the first parameter, and then returns the length of the new array. (Push: stack (stack), also known as stack, is a linear list with limited operations. Its restriction is that only insertion and deletion operations are allowed at one end of the list. This end is called the top of the stack. Relatively speaking, The other end is called the bottom of the stack. Inserting a new element into a stack is also called pushing, pushing or pushing. It puts the new element on top of the top element of the stack to make it the new top element; from a stack Deleting elements is also called popping or popping off the stack. It deletes the top element of the stack and makes its adjacent elements become new top elements of the stack.) This article introduces the syntax of the php array_push() function and some small examples. , let’s take a look together.
Definition and usage
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 .
array_push ( array &$array , mixed $value1 [, mixed $... ] ) : int
This function is equivalent to calling $array[] = $value multiple times.
Detailed explanation of syntax parameters
Parameters | Description |
---|---|
array | Required. Specifies an array. |
value1 | Required. Specifies the value to add. |
value2 | Optional. Specifies the value to add. |
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); ?>
Code running results:
Example 2
Use the php array_push() function to add characters Add elements to the array of string key names. The code is as follows:
<?php $a=array("a"=>"red","b"=>"green"); array_push($a,"blue","yellow"); print_r($a); ?>
Code running result:
[Related article recommendations]
Detailed explanation of php array_shift() function: delete the first element in the array
Detailed explanation of array_push(), array_pop() and array_shift() function usage examples in php
The above is the detailed content of php array_push() function. For more information, please follow other related articles on the PHP Chinese website!