Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function for php to append array elements

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge of what the function of php appends array elements is. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Four additional 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.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

To append an array element is to add an element at the end of the array. PHP has built-in functions to append array elements, which are described below.

1. Array_push () function

The array_push () function can insert one or more elements (key values) at the end of the array, with the following syntax:

Array_push ($array,$value1,$value2...)

Example:

2. Array_merge () function

The array_merge () function is used to merge one or more arrays into one array.

Array_merge (array1,array2,array3...)

Will array2,array3... Is stored in array1

Note: when a key in an input array already exists in the result array, array_merge () overwrites the previous key / value pair and replaces it with the key / value pair in the current input array.

Example:

3. Array_merge_recursive () function

The array_merge_recursive () function is the same as array_merge () above, and can combine two or more numbers together to form a joined array.

The difference is that when the same key name exists, array_merge () will overwrite the previous key / value pair, while array_merge_recursive () will merge the two values together to form a new array with the original key as the array name, and an array merge form, which is a recursive appended array.

Syntax:

Array_merge_recursive (array array1,array array2 [… , array arrayN])

Example:

4. Array_splice () function

The array_splice () function is a powerful function that can be used to delete, replace, or insert array elements (just set the parameter $length to 0). Syntax:

Array_splice ($array,$start,$length,$value)

When $length=0, the parameter $start specifies where to start the insertion (subscript)

The parameter $value specifies the insertion value (if it is multiple values, it needs to be set to an array).

When you set the value of $start to Array length value, that is, count ($arr), you can insert elements at the end of the array.

These are all the contents of the article "what is the function of php append array elements". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report