Home  >  Article  >  Backend Development  >  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

青灯夜游
青灯夜游Original
2022-06-14 18:05:171760browse

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...)
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(&#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);
?>

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);
?>

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!

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