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 numeric array:
Example 1
In this example, the ID key is automatically assigned:
The code is as follows: |
Copy code |
$ Names = array ("Peter", "Quagmire", "Joe ");
|
Example 2
In this example, we manually allocate the ID key:
The code is as follows: |
Copy code |
$ Names [0] = "Peter "; $ Names [1] = "Quagmire "; $ Names [2] = "Joe "; |
You can use these ID keys in the script:
The code is as follows: |
Copy code |
<? Php $ 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 code is as follows: |
Copy code |
$ 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 code is as follows: |
Copy code |
$ Ages ['Peter '] = "32 "; $ Ages ['quagmire'] = "30 "; $ Ages ['job'] = "34 "; |
You can use the ID key in the script:
The code is as follows: |
Copy code |
<? Php
$ 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 code is as follows: |
Copy code |
$ 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 code is as follows: |
Copy code |
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 code is as follows: |
Copy code |
<? Php $ 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 code is as follows: |
Copy code |
<? Php $ 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?
For example:
The code is as follows: |
Copy code |
<? Php $ Arr ['laruence '] = 'huixinchen '; $ Arr ['Yahoo '] = 2007; $ Arr ['baidu'] = 2008; Foreach ($ arr as $ key => $ val ){ // What is the result? } <Li> For example:
<? Php $ Arr [2] = 'huixinchen '; $ Arr [1] = 2007; $ Arr [0] = 2008; Foreach ($ arr as $ key => $ val ){ // What is the current result? } <Li> |