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

How to add elements to an php indexed array

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the "php index array how to add elements" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, hope that this "php index array how to add elements" article can help you solve the problem.

Method: 1, use "array_unshift (array, element)", add elements at the beginning of the array; 2, use "array_push (array, element)", add one or more elements at the end of the array; 3, use "array_splice (array, position value, 0, element)" to add elements anywhere in the array.

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

Several methods of adding elements to php indexed array

1. The array_unshift () function adds elements from the beginning of the array.

Array_unshift ($array,$value1,$value2...) Function can insert one or more new elements (key values) at the beginning of the array.

Let's take a look at the following examples:

Array_unshift ($arr,8,9) can see that two new elements are inserted at the beginning of the $arr array: the numeric value "8" and the string "9", so the output is:

Note: the array_unshift () function does not maintain the original numerical index relationship, but deletes all numeric key names and re-assigns them, that is, re-counting from 0.

2. The array_push () function adds elements from the end of the array.

Array_push ($array,$value1,$value2...) Function can insert one or more elements (key values) at the end of the array.

Let's take a look at the following examples:

Array_push ($arr,8, "9", 3.14) can see that three elements are inserted at the end of the $arr array: the integer "8", the string "9" and the floating point number "3.14", so the output is:

Unlike the array_unshift () function, the array_push () function does not reset the numeric key name, but counts based on the original numeric key name.

3. The array_splice () function inserts a new element from anywhere in the array.

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

When $length=0, the parameter $start specifies where to start the insertion (subscript), and the parameter $value specifies the insertion value (if it is multiple values, it needs to be set to an array).

Let's take a look at the following examples:

The output is as follows:

As you can see from the above example, when $start=0, the $value values are sequentially inserted at the beginning of the array. So what do you do if you want to insert an element at the end of an array?

Simply set the value of $start to "array length value", that is, count ($arr).

The output is as follows:

Insert the element from the specified location:

3. The array_pad () function adds elements from anywhere in the array.

The array_pad ($array,$size,$value) function inserts a key value of $value into the array $array, thus filling the array to a specified length of $size. The $size parameter can be understood as the final number of elements in the array, that is, the length of the array after the insert operation.

Let's take a look at the following examples:

As you can see from array_pad ($arr,5,1), if the value of $size is 5, there are five elements in the array after the insert operation, while there are already three elements, so you only need to insert two elements. And because the insert value $value is 1, you only need to insert two elements with a value of "1". So the output is:

As you can see from the above example, the array_pad () function can insert elements at the end of an array, while the array_pad () function can also insert elements at the beginning of an array; the key of this is the $size parameter.

There are three values for the $size parameter:

Is a positive number, the element is inserted at the end of the array

Is a negative number, the element is inserted at the beginning of the array

If its absolute value is less than or equal to the length of the $array array, no insert operation is performed.

The output is as follows:

The value of the parameter $value can also be an array, that is, insert an entire array, and the original array becomes a two-dimensional array.

This is the end of the introduction to "how to add elements to the php index array". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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