Home > Article > Backend Development > ThinkPHP learning--array learning
Instead of having many similar variables, you can store data as elements in an array. The elements in the array have their own IDs (i.e. subscripts) so they can be accessed easily. There are three array types: 1. Numeric array: an array with numeric ID keys; 2. Associative array: each ID key in the array is associated with a value; 3. Multidimensional array: an array containing one or more arrays.
Numeric array
Example 1 In this example, the ID key is automatically assigned:
Example 2
In this example, we manually assign the ID key:
You can use these ID keys in scripts:
Output of the above code: array1 or
array2 orarray3 ... Using numeric arrays is not the best practice when storing data about specifically named values. With associative arrays, we can use values as keys and assign values to them. Example 1In this example we use an array to assign age to different arrays:
Example 2This example is the same as Example 1, but shows Another way to create an array:
Of course you can also use the ID key in a script:
Output of the above script: array1 is 1 number.
Multidimensional ArrayIn a multidimensional array, each element in the main array is also an array. Each element in a subarray can also be an array, and so on. Example 1In this example, we create a multidimensional array with automatically assigned ID keys:
If you output this array, it should look like this :
The above introduces ThinkPHP learning-array learning, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.