Home > Article > Backend Development > Which built-in function in php will add value at the end of array
The built-in function array_push() in php will add a value to the end of the array. The array_push() function can insert one or more elements (push) to the end of the array and return the array length after inserting the new elements. The syntax is "array_push(array,value1,value2...)". The array_push() function treats the original array array as a stack and pushes the passed elements into the end of the array array; the length of the array will increase according to the number of elements pushed onto the stack.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
PHP array_push() : Insert elements at the end of the array
array_push() function is used to insert one or more elements (push) at the end of the array. Its syntax is as follows:
array_push(array,value1,value2...)
Parameters | Description |
---|---|
array | Required. Specifies an array. |
value1 | Required. Specifies the value to add. |
value2 | Optional. Specifies the value to add. |
Return value: Returns the length of the array after inserting the new element.
The array_push() function treats the original array array as a stack and pushes the incoming elements into the end of the array array; the length of the array will increase according to the number of elements pushed onto the stack.
Example 1:
<?php header('content-type:text/html;charset=utf-8'); $num = array(10, 45, 9); echo "原数组:"; var_dump($num); array_push($num, 100, 6); //在数组结尾插入元素 echo "插入多个元素后:"; var_dump($num); ?>
Note: Even if your array has string keys, the elements you add will have numeric keys.
Example 2:
<?php header('content-type:text/html;charset=utf-8'); $arr= array("a"=>"red","b"=>"green"); echo "原数组:"; var_dump($arr); array_push($arr, "blue","yellow"); //在数组结尾插入元素 echo "插入多个元素后:"; var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Which built-in function in php will add value at the end of array. For more information, please follow other related articles on the PHP Chinese website!