Home > Backend Development > PHP Problem > Which built-in function in php will add value at the end of array

Which built-in function in php will add value at the end of array

青灯夜游
Release: 2023-03-16 11:42:02
Original
1865 people have browsed it

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.

Which built-in function in php will add value at the end of array

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...)
Copy after login
ParametersDescription
arrayRequired. Specifies an array.
value1Required. 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(&#39;content-type:text/html;charset=utf-8&#39;);   
$num = array(10, 45, 9);
echo "原数组:";
var_dump($num);
array_push($num, 100, 6);  //在数组结尾插入元素
echo "插入多个元素后:";
var_dump($num);
?>
Copy after login

Which built-in function in php will add value at the end of array

Note: Even if your array has string keys, the elements you add will have numeric keys.

Example 2:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr= array("a"=>"red","b"=>"green");
echo "原数组:";
var_dump($arr);
array_push($arr, "blue","yellow");  //在数组结尾插入元素
echo "插入多个元素后:";
var_dump($arr);
?>
Copy after login

Which built-in function in php will add value at the end of array

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!

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