1. How to define an array: There are two main ways to create an array in PHP. Let's take a look at how to create an array
(1) Create an array directly to the method of assigning values to each element.
The format is: $arrayname[key]=value;
where arrayname is the name of the array, key is the key of the element of the array, and value is the value of the element. The key can be 0, 1, 2, 3, or a string. As shown below:
1 <?php
2 //Use the values of 1, 2, 3 as keys of the array
3 echo '<p>The key value of array $array1 is: </p>';
4 $array1[1]='a';
5 $array1[2]='b';
6 $array1[3]='c';
7 print_r($array1);
8
9 //If the key is omitted, the default key of the array is a value incremented from 0
10 echo '<p>The key value of array $array2 is:</p>';
11 $array2[]='a';
12 $array2[]='b';
13 $array2[]='c';
14 print_r($array2);
15
16 //A key with string as array
17 echo '<p>The key value of array $array3 is:</p>';
18 $array3['one']='a';
19 $array3['two']='b';
20 $array3['three']='c';
21 print_r($array3);
22 ?>
The output result of the above code is:
The key value of the array $array1 is:
Array ( [1] => a [2] => b [3] => c )
The key value of the array $array2 is:
Array ( [0] => a [1] => b [2] => c )
The key value of the array $array3 is:
Array ( [one] => a [two] => b [three] => c )
(2) Use the array function to directly define an array.
The format is: Earrayname=array(key1=>value1, key2=>value2);
where arrayname is the array name, key1 and key2 are the keys of the array, and value1 and value2 correspond to the values of key1 and key2 respectively.
Take an example, like the following code:
1 <?php
2 // Use the value as the key
3 $array6=array(1=>'a',2=>'b',3=>'c');
4 echo '<p>The keys and values of array $array6 are:</p>';
5 print_r($array6);
6 //Take string as key
7 $array7=array('one'=>'a','two'=>'b','three'=>'c');
8 echo '<p>The keys and values of array $array7 are:</p>';
9 print_r($array7);
10 //Writing method of omitting keys
11 $array8=array('a','b','c');
12 echo '<p>The keys and values of array $array8 are:</p>';
13 print_r($array8);
14 ?>
The result is:
The keys and values of the array $array6 are:
Array ( [1] => a [2] => b [3] => c )
The keys and values of the array $array7 are:
Array ( [one] => a [two] => b [three] => c )
The keys and values of the array $array8 are:
Array ( [0] => a [1] => b [2] => c )
Notice:
1> If you specify a value as its key for an element in the array, the default key of all elements after this element is the self-increasing non-repeat value of the specified value.
It's a bit difficult to understand simply by looking at the literal meaning, let's take a look at an example:
The following code:
1 <?php
2 //The key display of the first element of the array $array4 is specified as 2, and the following 2nd and 3rd elements are omitted in the way of keys
3 $array4[2]='a';
4 $array4[]='b';
5 $array4[]='c';
6 //The key display of the 4th element is specified as 10, and the subsequent 5th and 6th elements are omitted in the way of keys
7 $array4[10]='d';
8 $array4[]='e';
9 $array4[]='f';
10 //The key display of the 7th element is specified as 9, and the 8th and 9th elements are omitted in the way of keys.
11 $array4[9]='g';
12 $array4[]='h';
13 $array4[]='i';
14 //Print the keys and values of the array
15 print_r($array4);
16 ?>
The result is:
Array ( [2] => a [3] => b [4] => c [10] => d [11] => e [12] => f [9] => g [13] => h [14] => i )
Note: The key of the seventh element is 9. Under normal circumstances, the eighth element should be 10. However, if the keys 10, 11 and 12 have been used before, the keys of the eighth element are 13.
2> Whether it is a key with a number or a string as an array element, it represents only the key of this element, and has no direct relationship with the position of this element in the array. This is the biggest difference with arrays in languages such as C#. Here is an example.
The following code:
1 <?php
2 $array5['one']='a';
3 if(!isset($array5[0]))
4 {
5 echo '<p>$array5[0] is empty! </p>';
6 }
7 ?>
The result is:
$array5[0] is empty!
Note: $array5[0] represents the value of the element in the array with the key value 0 (not representing the first element of the array like C# and other languages). Since the array only has the key to the string ‘one’ element, and the key without the element is 0, so $array5[0] is empty.
3>PHP supports two types of arrays: indexed arrays and associated arrays. The former uses numbers as keys, and the latter uses strings as keys. When creating an array, you can use a mix of numbers and strings as keys for elements. The code shown below:
1 <?php
2 $array9=array(1=>'a', 2=>'b', 'one'=>'c', 'two'=>'d', 'e', 'f', 'g');
3 echo '<p>The keys and values of array $array9 are:</p>';
4 print_r($array9);
5 ?>
The result is:
The keys and values of the array $array9 are:
Array ( [1] => a [2] => b [one] => c [two] => d [3] => e [4] => f [5] => g )
4>Variables can also be used as keys to arrays, as shown below:
1 <?php
2 $key1='one';
3 $key2='two';
4 $key3='three';
5 $array10[$key1]='a';
6 $array10[$key2]='b';
7 $array10[$key3]='c';
8 echo '<p>The keys and values of array $array10 are:</p>';
9 print_r($array10);
10 ?>
The result is:
The keys and values of array $array10 are:
Array ( [one] => a [two] => b [three] => c )
2. How to access elements of an array
1. General methods
To get an element in an array, you only need to use the array name plus brackets and add a key. The call method is as follows:
$arrayname[key];
2. Use foreach results to traverse the array
If you want to access each array element, you can use a foreach loop:
Foreach($array as $value)
{
//Do something with $value
}
The Foreach loop will iterate over each element in the array $array and assign the value of each element to the $value variable. Here is an example:
1 <?php
2 $array11=array('a','b','c','d','e');
3 echo '<p>The value of array $array11 is:';
4 foreach($array11 as $value)
5 {
6 echo $value.',';
7 }
8 echo '</p>';
9 ?>
The output result is:
The value of array $array11 is: a,b,c,d,e,
Use foreach to access the keys and values of array elements at the same time, you can use:
Foreach($array as $key => $value)
{
//Do something with $key and $value
}
Where $key is the key of each element and the value of the $value element, the following code demonstrates how to create a drop-down box using the foreach structure:
1 <?php
2 $array12=array('one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5);
3 echo '<select name="onetofive">';
4 foreach($array12 as $key => $value)
5 {
6 echo "<option value=\"$value\">$key</option>";
7 }
8 echo '</select>';
9 ?>
3. Use list function to access the array
The List function assigns the values in the array to some variables, and its function syntax is as follows:
Void list(mixed varname, mixed varname2……)
See the following example:
1 <?php
2 $array13=array('red','blue','green');
3 // Assign value to all variables
4 list($flag1,$sky1,$grassland1)=$array13;
5 echo "$flag1 $sky1 $grassland1";
6 echo '<br>';
7 // Assign value to some variables
8 list($flag2,,$grassland2)=$array13;
9 echo "$flag2 $grassland2";
10 echo '<br>';
11 // Assign only to the third variable
12 list(,,$grassland3)=$array13;
13 echo "$grassland3";
14 echo '<br>';
15 ?>
The output result is:
red blue green
red green
green
Note: list() can only be used for arrays of numeric indexes and numeric indexes must start at 0.
Because the list function first assigns the element value with key 0 in the array to the first variable, then assigns the element value with key 1 to the second variable, and so on, the number and position of the variables in the list function must correspond to the numeric keys in the array to obtain the desired value, and the list function cannot access the array elements with strings as keys. As shown below:
1 <?php
2 $array13=array(1=>'red','blue','green');
3 list($flag1,$sky1,$grassland1)=$array13;
4 echo 'The value of $flag1 is: '.$flag1.'<br>';
5 echo '$sky1's value is: '.$sky1.'<br>';
6 echo '$grassland1's value is: '.$grassland1.'<br>';
7 ?>
The output result is:
The value of $flag1 is:
The value of $sky1 is: red
The value of $grassland1 is: blue
Note: Because the value of $flag1 should be the element value with the key of 0 in the array, but the first element of this array is a key with 1 and no element with the key of 0, the value of $flag1 is empty, which also causes the values of $sky1 and $grassland1 to change later.
4. Use each function to access the array
Each function returns the current key/value pair in the array and moves the array pointer one step forward. Note that it is a pair, as explained in detail below. The function syntax:
array each ( array &$array )
Returns the key/value pair of the current pointer position in the array array and moves the array pointer forward. Each key-value pair is returned as an array of four units, with the key values of 0, 1, key and value. Elements 0 and key contain the key names of the array units, and 1 and value contain the data. If the inner pointer passes the end of the array, each() returns FALSE. Why are there four following tables for each function? In fact, the each function gets these four subscripts just for us to operate. We can use 0 and 1 as the index, or key and value as the index. Please see the following code:
1 <?php
2 $arr=array("I am the first value", "I am the second value", "I am the third value");
3 echo "When we use 0 and 1 as the index: <br/><br/>";
4 $a=each($arr);
5 echo "My key in the \$arr array is: ".$a['0'];
6 echo "<br/>";
7 echo "My value in the \$arr array is: ".$a['1'];
8 echo "<br/><br/>";
9 echo "When we use key, value as index: <br/><br/>";
10 $b=each($arr);
11 echo "My key in the \$arr array is: ".$b['key'];
12 echo "<br/>";
13 echo "My value in the \$arr array is: ".$b['value'];
14 ?>
Shown as:
When we use 0 and 1 as the index:
My key in the $arr array is: 0
My value in the $arr array is: I'm the first value
When we use key and value as index:
My key in the $arr array is: 1
My value in the $arr array is: I'm the second value
5. Use each function and list function to traverse the array, as shown in the following example:
1 <?php
2 $array14=array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
3 while(list($key,$value) = each($array14))
4 {
5 echo "$key => $value\n";
6 }
7 ?>
The output result is:
a => apple b => banana c => cranberry
6. Use for for to access the array
As shown in the following example:
1 <?php
2 $array15=array('a','b','c','d','e','f');
3 for($i=0;$i<count($array15);$i++)
4 {
5 echo 'array element: '.$array15[$i].'<br>';
6 }
7 ?>
The output result is:
Array elements: a
Array elements: b
Array elements: c
Array element: d
Array elements: e
Array element: f
(1) Create an array directly to the method of assigning values to each element.
The format is: $arrayname[key]=value;
where arrayname is the name of the array, key is the key of the element of the array, and value is the value of the element. The key can be 0, 1, 2, 3, or a string. As shown below:
Copy the codeThe code is as follows:
1 <?php
2 //Use the values of 1, 2, 3 as keys of the array
3 echo '<p>The key value of array $array1 is: </p>';
4 $array1[1]='a';
5 $array1[2]='b';
6 $array1[3]='c';
7 print_r($array1);
8
9 //If the key is omitted, the default key of the array is a value incremented from 0
10 echo '<p>The key value of array $array2 is:</p>';
11 $array2[]='a';
12 $array2[]='b';
13 $array2[]='c';
14 print_r($array2);
15
16 //A key with string as array
17 echo '<p>The key value of array $array3 is:</p>';
18 $array3['one']='a';
19 $array3['two']='b';
20 $array3['three']='c';
21 print_r($array3);
22 ?>
The output result of the above code is:
The key value of the array $array1 is:
Array ( [1] => a [2] => b [3] => c )
The key value of the array $array2 is:
Array ( [0] => a [1] => b [2] => c )
The key value of the array $array3 is:
Array ( [one] => a [two] => b [three] => c )
(2) Use the array function to directly define an array.
The format is: Earrayname=array(key1=>value1, key2=>value2);
where arrayname is the array name, key1 and key2 are the keys of the array, and value1 and value2 correspond to the values of key1 and key2 respectively.
Take an example, like the following code:
Copy the codeThe code is as follows:
1 <?php
2 // Use the value as the key
3 $array6=array(1=>'a',2=>'b',3=>'c');
4 echo '<p>The keys and values of array $array6 are:</p>';
5 print_r($array6);
6 //Take string as key
7 $array7=array('one'=>'a','two'=>'b','three'=>'c');
8 echo '<p>The keys and values of array $array7 are:</p>';
9 print_r($array7);
10 //Writing method of omitting keys
11 $array8=array('a','b','c');
12 echo '<p>The keys and values of array $array8 are:</p>';
13 print_r($array8);
14 ?>
The result is:
The keys and values of the array $array6 are:
Array ( [1] => a [2] => b [3] => c )
The keys and values of the array $array7 are:
Array ( [one] => a [two] => b [three] => c )
The keys and values of the array $array8 are:
Array ( [0] => a [1] => b [2] => c )
Notice:
1> If you specify a value as its key for an element in the array, the default key of all elements after this element is the self-increasing non-repeat value of the specified value.
It's a bit difficult to understand simply by looking at the literal meaning, let's take a look at an example:
The following code:
Copy the codeThe code is as follows:
1 <?php
2 //The key display of the first element of the array $array4 is specified as 2, and the following 2nd and 3rd elements are omitted in the way of keys
3 $array4[2]='a';
4 $array4[]='b';
5 $array4[]='c';
6 //The key display of the 4th element is specified as 10, and the subsequent 5th and 6th elements are omitted in the way of keys
7 $array4[10]='d';
8 $array4[]='e';
9 $array4[]='f';
10 //The key display of the 7th element is specified as 9, and the 8th and 9th elements are omitted in the way of keys.
11 $array4[9]='g';
12 $array4[]='h';
13 $array4[]='i';
14 //Print the keys and values of the array
15 print_r($array4);
16 ?>
The result is:
Array ( [2] => a [3] => b [4] => c [10] => d [11] => e [12] => f [9] => g [13] => h [14] => i )
Note: The key of the seventh element is 9. Under normal circumstances, the eighth element should be 10. However, if the keys 10, 11 and 12 have been used before, the keys of the eighth element are 13.
2> Whether it is a key with a number or a string as an array element, it represents only the key of this element, and has no direct relationship with the position of this element in the array. This is the biggest difference with arrays in languages such as C#. Here is an example.
The following code:
Copy the codeThe code is as follows:
1 <?php
2 $array5['one']='a';
3 if(!isset($array5[0]))
4 {
5 echo '<p>$array5[0] is empty! </p>';
6 }
7 ?>
The result is:
$array5[0] is empty!
Note: $array5[0] represents the value of the element in the array with the key value 0 (not representing the first element of the array like C# and other languages). Since the array only has the key to the string ‘one’ element, and the key without the element is 0, so $array5[0] is empty.
3>PHP supports two types of arrays: indexed arrays and associated arrays. The former uses numbers as keys, and the latter uses strings as keys. When creating an array, you can use a mix of numbers and strings as keys for elements. The code shown below:
Copy the codeThe code is as follows:
1 <?php
2 $array9=array(1=>'a', 2=>'b', 'one'=>'c', 'two'=>'d', 'e', 'f', 'g');
3 echo '<p>The keys and values of array $array9 are:</p>';
4 print_r($array9);
5 ?>
The result is:
The keys and values of the array $array9 are:
Array ( [1] => a [2] => b [one] => c [two] => d [3] => e [4] => f [5] => g )
4>Variables can also be used as keys to arrays, as shown below:
Copy the codeThe code is as follows:
1 <?php
2 $key1='one';
3 $key2='two';
4 $key3='three';
5 $array10[$key1]='a';
6 $array10[$key2]='b';
7 $array10[$key3]='c';
8 echo '<p>The keys and values of array $array10 are:</p>';
9 print_r($array10);
10 ?>
The result is:
The keys and values of array $array10 are:
Array ( [one] => a [two] => b [three] => c )
2. How to access elements of an array
1. General methods
To get an element in an array, you only need to use the array name plus brackets and add a key. The call method is as follows:
$arrayname[key];
2. Use foreach results to traverse the array
If you want to access each array element, you can use a foreach loop:
Foreach($array as $value)
{
//Do something with $value
}
The Foreach loop will iterate over each element in the array $array and assign the value of each element to the $value variable. Here is an example:
Copy the codeThe code is as follows:
1 <?php
2 $array11=array('a','b','c','d','e');
3 echo '<p>The value of array $array11 is:';
4 foreach($array11 as $value)
5 {
6 echo $value.',';
7 }
8 echo '</p>';
9 ?>
The output result is:
The value of array $array11 is: a,b,c,d,e,
Use foreach to access the keys and values of array elements at the same time, you can use:
Foreach($array as $key => $value)
{
//Do something with $key and $value
}
Where $key is the key of each element and the value of the $value element, the following code demonstrates how to create a drop-down box using the foreach structure:
Copy the codeThe code is as follows:
1 <?php
2 $array12=array('one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5);
3 echo '<select name="onetofive">';
4 foreach($array12 as $key => $value)
5 {
6 echo "<option value=\"$value\">$key</option>";
7 }
8 echo '</select>';
9 ?>
3. Use list function to access the array
The List function assigns the values in the array to some variables, and its function syntax is as follows:
Void list(mixed varname, mixed varname2……)
See the following example:
Copy the codeThe code is as follows:
1 <?php
2 $array13=array('red','blue','green');
3 // Assign value to all variables
4 list($flag1,$sky1,$grassland1)=$array13;
5 echo "$flag1 $sky1 $grassland1";
6 echo '<br>';
7 // Assign value to some variables
8 list($flag2,,$grassland2)=$array13;
9 echo "$flag2 $grassland2";
10 echo '<br>';
11 // Assign only to the third variable
12 list(,,$grassland3)=$array13;
13 echo "$grassland3";
14 echo '<br>';
15 ?>
The output result is:
red blue green
red green
green
Note: list() can only be used for arrays of numeric indexes and numeric indexes must start at 0.
Because the list function first assigns the element value with key 0 in the array to the first variable, then assigns the element value with key 1 to the second variable, and so on, the number and position of the variables in the list function must correspond to the numeric keys in the array to obtain the desired value, and the list function cannot access the array elements with strings as keys. As shown below:
Copy the codeThe code is as follows:
1 <?php
2 $array13=array(1=>'red','blue','green');
3 list($flag1,$sky1,$grassland1)=$array13;
4 echo 'The value of $flag1 is: '.$flag1.'<br>';
5 echo '$sky1's value is: '.$sky1.'<br>';
6 echo '$grassland1's value is: '.$grassland1.'<br>';
7 ?>
The output result is:
The value of $flag1 is:
The value of $sky1 is: red
The value of $grassland1 is: blue
Note: Because the value of $flag1 should be the element value with the key of 0 in the array, but the first element of this array is a key with 1 and no element with the key of 0, the value of $flag1 is empty, which also causes the values of $sky1 and $grassland1 to change later.
4. Use each function to access the array
Each function returns the current key/value pair in the array and moves the array pointer one step forward. Note that it is a pair, as explained in detail below. The function syntax:
array each ( array &$array )
Returns the key/value pair of the current pointer position in the array array and moves the array pointer forward. Each key-value pair is returned as an array of four units, with the key values of 0, 1, key and value. Elements 0 and key contain the key names of the array units, and 1 and value contain the data. If the inner pointer passes the end of the array, each() returns FALSE. Why are there four following tables for each function? In fact, the each function gets these four subscripts just for us to operate. We can use 0 and 1 as the index, or key and value as the index. Please see the following code:
Copy the codeThe code is as follows:
1 <?php
2 $arr=array("I am the first value", "I am the second value", "I am the third value");
3 echo "When we use 0 and 1 as the index: <br/><br/>";
4 $a=each($arr);
5 echo "My key in the \$arr array is: ".$a['0'];
6 echo "<br/>";
7 echo "My value in the \$arr array is: ".$a['1'];
8 echo "<br/><br/>";
9 echo "When we use key, value as index: <br/><br/>";
10 $b=each($arr);
11 echo "My key in the \$arr array is: ".$b['key'];
12 echo "<br/>";
13 echo "My value in the \$arr array is: ".$b['value'];
14 ?>
Shown as:
When we use 0 and 1 as the index:
My key in the $arr array is: 0
My value in the $arr array is: I'm the first value
When we use key and value as index:
My key in the $arr array is: 1
My value in the $arr array is: I'm the second value
5. Use each function and list function to traverse the array, as shown in the following example:
Copy the codeThe code is as follows:
1 <?php
2 $array14=array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
3 while(list($key,$value) = each($array14))
4 {
5 echo "$key => $value\n";
6 }
7 ?>
The output result is:
a => apple b => banana c => cranberry
6. Use for for to access the array
As shown in the following example:
Copy the codeThe code is as follows:
1 <?php
2 $array15=array('a','b','c','d','e','f');
3 for($i=0;$i<count($array15);$i++)
4 {
5 echo 'array element: '.$array15[$i].'<br>';
6 }
7 ?>
The output result is:
Array elements: a
Array elements: b
Array elements: c
Array element: d
Array elements: e
Array element: f