In php, arrays are a very important data type. they can store different types of data. compared with other simple integer types, arrays can only store a single data type, which is much better. next I will introduce php Data
In php, arrays are a very important data type. they can store different types of data. compared with other simple integer types, arrays can only store a single data, next I will introduce php array usage. PHP array is one of the most important knowledge. it can store one or more values in a single variable name.
What is an array?
Many similar variables need to be created during PHP development. without many similar variables, you can store data as elements in an array. all elements in the array have their own IDs, so they can be easily accessed.
There are three types of arrays:
Numeric array
Array with digit ID key
Join array
Each ID key in the array is associated with a value.
Multi-dimensional array
Array containing one or more arrays
Numeric array
Each element stored in the value array has a digital ID key. you can use different methods to create a value array:
Example 1
In this example, the ID key is automatically assigned:
The instance code is as follows:
- $ Names = array ("Peter", "Quagmire", "Joe ");
Example 2
In this example, we manually allocate the ID key:
The instance code is as follows:
- $ Names [0] = "Peter ";
- $ Names [1] = "Quagmire ";
- $ Names [2] = "Joe ";
You can use these ID keys in the script:
The instance code is as follows:
-
- $ Names [0] = "Peter ";
- $ Names [1] = "Quagmire ";
- $ Names [2] = "Joe ";
- Echo $ names [1]. "and". $ names [2]. "are". $ names [0]. "'s neighbors ";
- ?>
Output of the above code:
1 Quagmire and Joe are Peter's neighbors
Join array
Join array. each ID key is associated with a value. it is not the best practice to use a numeric array to store data related to specific named values. by associating arrays, we can use values as keys and assign values to them.
Example 1
In this example, we use an array to allocate ages to different people:
The instance code is as follows:
- $ Ages = array ("Peter" => 32, "Quagmire" => 30, "Joe" => 34 );
Example 2
This example is the same as example 1, but shows another method to create an array:
The instance code is as follows:
- $ Ages ['Peter '] = "32 ";
- $ Ages ['quagmire'] = "30 ";
- $ Ages ['job'] = "34 ";
You can use the ID key in the script:
The instance code is as follows:
-
- $ Ages ['Peter '] = "32 ";
- $ Ages ['quagmire'] = "30 ";
- $ Ages ['job'] = "34 ";
- Echo "Peter is". $ ages ['Peter ']. "years old .";
- ?>
Output of the above script:
Peter is 32 years old.
Multi-dimensional array
In a multi-dimensional array, each element in the main array is also an array. each element in the sub-array can also be an array, and so on.
Example 1
In this example, we create a multidimensional array with an automatically assigned ID key:
The instance code is as follows:
- $ Families = array
-
- (
- "Griffin in" => array
- (
- "Peter ",
- "Lois ",
- "Megan"
- ),
- "Quagmire" => array
- (
- "Glenn"
- ),
- "Brown" => array
- (
- "Cleveland ",
- "Loretta ",
- "Junior"
- )
- );
If this array is output, it should be similar to the following:
- Array
- (
- [Griffin in] => Array
- (
- [0] => Peter
- [1] => Lois
- [2] => Megan
- )
- [Quagmire] => Array
- (
- [0] => Glenn
- )
- [Brown] => Array
- (
- [0] => Cleveland
- [1] => Loretta
- [2] => Junior
- )
- )
Example 2
Let's try to display a single value in the above array:
The instance code is as follows:
- Echo "Is". $ families ['grigging'] [2].
- "A part of the Griffin family? ";
Output of the above code:
Is Megan a part of the Griffin family?
For PHP content, I would like to share it with you briefly. I hope you will be helpful when modifying the WordPress theme. lei Zi is also learning and doing it. he does not know much about it in many places and hopes to improve himself.
Display of array elements
In the above example, no matter $ people [2] or $ minutes les ['CS '], only an array element value with a known clear position is output, how to quickly output all or part of the array elements is undoubtedly the fastest way to use loop statements.
The instance code is as follows:
-
- $ People = array ('name', 'sex', 'nation', 'birth ');
- For ($ I = 0; $ I <4; $ I ++)
- Echo "$ people [$ I]";
- ?>
In addition to the for loop that understands the number of loops, you can also use a foreach statement that does not require the number of loops.
The instance code is as follows:
-
- $ People = array ('name', 'sex', 'nation', 'birth ');
- Foreach ($ people as $ xiangmu)
- Echo $ xiangmu;
- ?>
Array traversal
When using foreach for access, is the traversal order fixed? In what order does it traverse?
The instance code is as follows:
-
- $ Arr ['laruence '] = 'huixinchen ';
- $ Arr ['Yahoo '] = 2007;
- $ Arr ['baidu'] = 2008;
- Foreach ($ arr as $ key => $ val ){
- // What is the result?
- } <Li>
For example:
-
- $ Arr [2] = 'huixinchen ';
- $ Arr [1] = 2007;
- $ Arr [0] = 2008;
- Foreach ($ arr as $ key => $ val ){
- // What is the current result?
- } <Li>