Home > Article > Backend Development > What are the functions for appending array elements in php?
4 kinds of append functions: 1. array_push(), syntax "array_push(array, element value list...)"; 2. array_merge(), syntax "array_merge(array 1, array 2...)" "; 3. array_merge_recursive(), etc.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
The so-called appending array elements means adding them to the end of the array Add elements. PHP has a variety of built-in functions for appending array elements, which are introduced below.
1. array_push() function
array_push() function can insert one or more elements (key values) at the end of the array. The syntax is as follows:
array_push($array,$value1,$value2...)
Example:
<?php $arr=array(1,2,3); array_push($arr,8,"9",3.14); var_dump($arr); ?>
2. array_merge() function
array_merge() function is used to merge an Or multiple arrays are combined into one array
array_merge(array1,array2,array3...)
will store the values of array2, array3... into array1.
Note: When a key in an input array already exists in the result array, array_merge() will overwrite the previously existing key/value pair and replace it with the current input Key/value pairs in an array.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20); $result =array_merge($arr,array(2,3,4)) ; var_dump($result); ?>
3. array_merge_recursive() function
array_merge_recursive() function and array_merge above () are the same and can combine two or more arrays together to form a combined array.
The difference is: when the same key name exists, array_merge() will overwrite the previously existing key/value pair, while array_merge_recursive() will merge the two values together to form a new array, with the original Some keys are used as array names, and there is also a form of array merging, which is to recursively append arrays.
Syntax:
array_merge_recursive(array array1,array array2[…,array arrayN])
Example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20); $result =array_merge_recursive($arr,array(2,3,4,8)) ; var_dump($result); ?>
4, array_splice() function
The array_splice() function is a powerful function that can be used to delete array elements, replace array elements, and also insert array elements (just set the parameter $length to 0). Syntax:
array_splice($array,$start,$length,$value)
When $length=0, then the parameter $start can specify the position (subscript) to start inserting
Parameter$ value can specify the insertion value (if there are multiple values, it needs to be set as an array).
When the value of $start is set to the "array length value", that is, count($arr) can insert elements at the end of the array.
<?php $arr=array(1,2,3); array_splice($arr,count($arr),0,"1"); var_dump($arr); array_splice($arr,count($arr),0,array(25,"3")); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the functions for appending array elements in php?. For more information, please follow other related articles on the PHP Chinese website!