Home  >  Article  >  Backend Development  >  ThinkPHP learning--array learning

ThinkPHP learning--array learning

WBOY
WBOYOriginal
2016-07-29 08:56:201048browse

                                                                                                                                                                                        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:

ThinkPHP learning--array learning Example 2

In this example, we manually assign the ID key:


ThinkPHP learning--array learning You can use these ID keys in scripts:

ThinkPHP learning--array learningOutput of the above code: array1 or

array2 or

array3 ... 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: ThinkPHP learning--array learning


Of course you can also use the ID key in a script: ThinkPHP learning--array learning

Output of the above script: array1 is 1 number.ThinkPHP learning--array learning

Multidimensional Array

In 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 :ThinkPHP learning--array learning

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn