What are the functions for appending array elements in php?

青灯夜游
Release: 2023-03-16 07:38:01
Original
6206 people have browsed it

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.

What are the functions for appending array elements in php?

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...)
Copy after login

Example:

Copy after login

What are the functions for appending array elements in php?

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...)
Copy after login
  • 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:

Copy after login

What are the functions for appending array elements in php?

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])
Copy after login

Example:

Copy after login

What are the functions for appending array elements in php?

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)
Copy after login
  • 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.

Copy after login

What are the functions for appending array elements in php?

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!