In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what are the methods of defining arrays in php". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the methods of defining arrays in php"?
Two methods: 1, directly assign values to array elements, the syntax is "$array variable name [subscript] = value;", the "subscript" can be a string, an integer, or empty; 2, use the array () function to create an array, syntax "$array variable name = array (key 1 = > value 1, key 2 = > value 2.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Defining arrays in PHP is very flexible, and unlike arrays in many other programming languages, PHP does not need to specify the size of the array when creating the array, or even declare it before using the array, and can store any type of data in the same array.
PHP defines an array in the following two ways:
Directly assign values to array elements
Use the array () function
The following is an actual code example to introduce these two methods in detail.
1. Assign values to array elements directly
We can create and initialize an array in a format such as "$array variable name [subscript] = value;"
The subscript can be a string, an integer, or empty (that is, no specific index value is specified).
Example 1:
Output result:
Example 2:
Output result:
You can see that when we do not specify a specific index value in square brackets, the default is a numeric index, and the index value increases sequentially from 0 by default.
Tip: in addition to using the var_dump () function to print the entire array, you can also use the print_r () function.
There is no size limit for arrays in PHP, so in the above array, you can continue to add new elements to the array in the same way. You can access the elements in the array by "$array variable name [subscript]". The example code is as follows:
2. Use the array () function
Another way to define 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)
1) directly array () function, without adding any parameters, you can create an empty array
Output:
2) if you do not specify a subscript using the = > symbol, it defaults to an indexed array. The default index value is also incremented from 0.
Output:
3) the array () function also accepts a number of key= > value parameter pairs separated by commas, which defines an associative array.
Output result:
At this point, I believe you have a deeper understanding of "what are the methods of defining arrays in php?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.