In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the methods of creating a php array". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the methods of creating an php array"?
The method of creating an array: 1, use the "$array name [subscript] = value;" statement, to directly assign values to create the array; 2, use the "$array name = array (key name = > value);" statement; 3, use array_fill () to fill the key value to create a new array; 4, use array_combine (), and so on.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
1. Create an array by directly assigning values
The syntax format for declaring an one-dimensional array is as follows:
Array variable name [subscript] = value
Where the subscript (index value) can be a string or an integer, and the subscript needs to be wrapped with [].
When declaring an index array, if the index value is incremented, we can also not specify a specific index value in square brackets, where the index value increases by default starting at 0.
2. Use the array () function to create an array
Another way to declare an array is to use the array () function to create a new array. It accepts a certain number of key= > value parameter pairs separated by commas. The syntax format is as follows:
Array variable name = array (key1 = > value1, key2 = > value2,..., keyN = > valueN)
Example 1:
Example 2:
3. Create an array using array_fill ()
Array_fill (): creates a new array by populating the key value
The array_fill ($index, $number, $value) function takes three parameters that cannot be omitted: $index (the starting index), $number (the number of populated elements), and $value (the key value used to populate).
The $index parameter supports negative values, and the array index has the following three values:
If positive, the array index starts with the $index value and ends with the $index+$number- 1 value. If $index is 2 and the number is 4, then the array index is: 2, 3, 4, 5.
If 0, the array index starts at 0 and ends with the $number- 1 value. For example, $index is 0 and the number is 4, then the array index is 0, 1, 2, 3.
If negative, the array index consists of $index,0,1,2,..., and $number-2. If $index is-2 and the number is 4, the array index is-2, 0, 1, 2.
In the above example, the $index of array_fill (0value 4, "hello") is 0 (the index starts at 0), and the $number is 4 (the array has four elements), so the array index is 0, 1, 2, 3; while $value is hello, the key values of all four elements are hello, so the output is:
Looking at two examples:
Analysis: the index starts at 3 and ends at 3: 4-1: 6, and the key value of the array is hello, so the output is:
The output is as follows:
The $value parameter can be either a single numeric value / string or an array, so a two-dimensional array is created.
The output is as follows:
4. Use the array_fill_keys () function
Array_fill_keys (): creates a new array by populating the key name and key value
The array_fill_keys ($keys,$value) function takes two unomitted arguments $keys (an array of populated key names) and $value (populated key values).
Simply put, the array_fill_keys () function populates a new array with elements in the $keys array as the key name and $value as the value.
As you can see from the code example above, the new array has four elements with key names of "a", "b", "c", and "d", respectively; the key values of all four elements are "hello", so the output is:
Array_fill_keys () and array_fill () function, $value value can also accept an array, so the creation of a two-dimensional array.
The output is as follows:
5. Use the array_combine () function
The array_combine () function creates a new array by combining two arrays, one an array of key names and the other an array of key values.
The output is as follows:
As you can see, in the above example, the array_combine ($keys,$values) function is used to create a new array by merging two arrays, where the element in the $keys array is the key name of the new array, and the element of the $values array is the key value of the new array.
It is important to note that:
When using the array_combine () function to create an array, the number of elements in the $keys array and the $values array must be the same, so that the key name and key value correspond one to one, otherwise an error will be reported and FALSE will be returned.
The $keys array cannot be a multidimensional array, but the $values array can be a multidimensional array.
6. Use the range () function
The range () function creates an array of elements in the specified range.
Syntax:
Range (low,high,step)
Low is required. Specifies the minimum value of the array elements.
High is required. Specifies the maximum value of the array elements.
Step is optional. Specifies the step-by-step between elements. The default is 1.
This function returns an array of elements from low to high.
Thank you for your reading, these are the contents of "what are the methods of creating php arrays". After the study of this article, I believe you have a deeper understanding of what the methods of creating php arrays have, 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.