In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "php how to add an array element to the array", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php how to add an array element to the array" bar!
The method of adding elements: 1, use the "array_unshift (array, array elements)" statement, add elements at the beginning of the array; 2, use the "array_push (array, array elements)" statement, add elements at the end of the array; 3, use the "array_pad (array, array length + 1, element)" statement.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Php's method of adding an array element to an array
1. The array_unshift () function inserts new elements into 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:
The array_unshift () function does not maintain the original numeric index relationship, but removes all numeric key names and re-assigns them, that is, re-counting from 0; but all string key names remain the same.
Output result:
2. The array_push () function inserts a new element into 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:
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.
Output result:
3. The array_pad () function inserts a new element into 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.
Simply set the $size parameter to the original array length + 1 to insert a new element
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.
The output is as follows:
Thank you for reading, the above is the content of "php how to add an array element to the array". After the study of this article, I believe you have a deeper understanding of how php adds an array element to the array, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.