PHP: an array of notes

  • 2020-05-10 17:46:39
  • OfStack

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 by assigning values to each element directly.

Format: $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 keys can be Numbers like 0,1,2,3, or they can be strings. As follows:
 
1 <?php 
2 // with 1 . 2 . 3 Is used as the key of the array  
3 echo '<p> An array of $array1 The key value of: </p>'; 
4 $array1[1]='a'; 
5 $array1[2]='b'; 
6 $array1[3]='c'; 
7 print_r($array1); 
8 
9 // If the key is omitted in this way, the default key of the array is from 0 Start incrementing  
10 echo '<p> An array of $array2 The key value of: </p>'; 
11 $array2[]='a'; 
12 $array2[]='b'; 
13 $array2[]='c'; 
14 print_r($array2); 
15 
16 // Takes a string as the key of the array  
17 echo '<p> An array of $array3 The key value of: </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 array $array1 is:

Array ( [1] = > a [2] = > b [3] = > c )
The key value of array $array2 is:

Array ( [0] = > a [1] = > b [2] = > c )
The key value of array $array3 is:

Array ( [one] = > a [two] = > b [three] = > c )

(2) directly define an array with the array function.

The format is: $arrayname = array (key1 = > value1, key2 = > value2);

Where arrayname is the name of the array, key1 and key2 are the keys of the array, value1 and value2 respectively correspond to the values of key1 and key2.

Take an example, such as the following code:
 
1 <?php 
2 // Using a number as a key  
3 $array6=array(1=>'a',2=>'b',3=>'c'); 
4 echo '<p> An array of $array6 The keys and values of: </p>'; 
5 print_r($array6); 
6 // Take a string as the key  
7 $array7=array('one'=>'a','two'=>'b','three'=>'c'); 
8 echo '<p> An array of $array7 The keys and values of: </p>'; 
9 print_r($array7); 
10 // A way of writing an omitted key  
11 $array8=array('a','b','c'); 
12 echo '<p> An array of $array8 The keys and values of: </p>'; 
13 print_r($array8); 
14 ?> 

The results are:

The keys and values of 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 )

Note:

If 1 > specifies a numeric value as its key for an element in an array, the default key for all elements following that element is a self-incrementing, non-repeating value of the specified value.

It's a little hard to take it literally, but let's take a look at an example:

The following code:
 
1 <?php 
2 // An array of $array4 The first 1 The key display of the element is specified as 2 And then the first 2 , 3 Element by omitting the key  
3 $array4[2]='a'; 
4 $array4[]='b'; 
5 $array4[]='c'; 
6 // The first 4 The key display of the element is specified as 10 And then the first 5 , 6 Element by omitting the key  
7 $array4[10]='d'; 
8 $array4[]='e'; 
9 $array4[]='f'; 
10 // The first 7 The key display of the element is specified as 9 And then the first 8 , 9 Element by omitting the key  
11 $array4[9]='g'; 
12 $array4[]='h'; 
13 $array4[]='i'; 
14 // Prints the keys and values of the array  
15 print_r($array4); 
16 ?> 

The results are:

Array ( [2] = > A [3] = > [4] = b > [10] = c > D [11] = > E = [12] > [9] = f > G = [13] > H = [14] > I)

Note: the key of the seventh element is 9. Normally, the key between the eighth element should be 10, but the keys 10, 11 and 12 have been used before, so the key of the eighth element is 13.

2 >, whether it is a number or a string as the key of an array element, represents only the key of that element and has no direct relationship with its position in the array, which is the biggest difference from an array in C# and other languages. Here's 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 results are:

$array5[0] is empty!

Note: $array5[0] represents the value of the element in the array whose key is 0 (unlike C#, which represents the first element of the array). Since the array has only the 1 element whose key is the string 'one', and no key is 0, $array5[0] is empty.

3 > PHP supports two types of arrays: indexed arrays (indexed array) and associative arrays (associative array), with the former using Numbers as keys and the latter using strings as keys. You can mix Numbers and strings as the keys of elements when creating an array. The following code is shown:
 
1 <?php 
2 $array9=array(1=>'a', 2=>'b', 'one'=>'c', 'two'=>'d', 'e', 'f', 'g'); 
3 echo '<p> An array of $array9 The keys and values of: </p>'; 
4 print_r($array9); 
5 ?> 

The results are:

The keys and values of array $array9 are:

Array ( [1] = > a [2] = > b [one] = > c [two] = > d [3] = > e [4] = > f [5] = > g )

The 4 > variable can also be used as an array key, 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> An array of $array10 The keys and values of: </p>'; 
9 print_r($array10); 
10 ?> 

The results are:

The keys and values of the array $array10 are:

Array ( [one] = > a [two] = > b [three] = > c )

2. How do I access the elements of an array

1. General methods

To get an element in an array, simply use the array name plus parentheses plus a key, and call the method as follows:

$arrayname[key];

2. Iterate over groups using foreach results

If you want to access each array element, you can use the 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> An array of $array11 The value is: '; 
4 foreach($array11 as $value) 
5 { 
6 echo $value.','; 
7 } 
8 echo '</p>'; 
9 ?> 

The output result is:

Array $array11 value is: a, b, c, d, e,



You can also access the keys and values of array elements at the same time using foreach. You can use:

Foreach ($array as $key = > $value)

{

//Do something with $key and $value

}

Where $key is the key of each element and $value is the value of the 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 the list function to access the array

The List function assigns the value of the array to some variables. The syntax of the function is as follows:

(mixed varname, mixed varname2...)

See the following example:
 
1 <?php 
2 $array13=array('red','blue','green'); 
3 // Assign values to all variables  
4 list($flag1,$sky1,$grassland1)=$array13; 
5 echo "$flag1 $sky1 $grassland1"; 
6 echo '<br>'; 
7 // Assign values to partial variables  
8 list($flag2,,$grassland2)=$array13; 
9 echo "$flag2 $grassland2"; 
10 echo '<br>'; 
11 // Assign only to the first 3 A 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 numeric indexed arrays and the numeric index must start at 0.

Because list function is the key to zero element in the array first value assigned to the first variable, and the key elements of 1 value assigned to the second variable, and so on, so list function of variable number and position must correspond and array of numeric keys, and want to get value, and list function is not access to string as the key of array elements. As follows:
 
1 <?php 
2 $array13=array(1=>'red','blue','green'); 
3 list($flag1,$sky1,$grassland1)=$array13; 
4 echo '$flag1 The value is: '.$flag1.'<br>'; 
5 echo '$sky1 The value is: '.$sky1.'<br>'; 
6 echo '$grassland1 The 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 have been the value of the element with the key of 0 in the array, the value of $flag1 is empty because the first element of this array has a key of 1 and there is no element with the key of 0, so the value of $sky1 and $grassland1 have changed accordingly.

4. Use the each function to access the array

The each function returns the current key/value pair in the array and moves the array pointer 1 step forward. Note that the pair is 1, as described below. The function syntax:

array each ( array &$array )

Returns the key/value pair at 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 cells with key values of 0,1, key and value4 elements. The elements 0 and key contain the key names of the array cells, and 1 and value contain the data. If the internal pointer passes over the end of the array, each() returns FALSE. Why does each have four tables? The fact that each gets these four indices is just for our convenience. We can use 0,1 as the index, or key,value as the index. See the following code:
 
1 <?php 
2 $arr=array(" I am the first 1 A value "," I am the first 2 A value "," I am the first 3 A value "); 
3 echo " When we use 0,1 When is the index: <br/><br/>"; 
4 $a=each($arr); 
5 echo " I am in \$arr The keys in the array are: ".$a['0']; 
6 echo "<br/>"; 
7 echo " I am in \$arr The values in the array are: ".$a['1']; 
8 echo "<br/><br/>"; 
9 echo " When we use key,value When is the index: <br/><br/>"; 
10 $b=each($arr); 
11 echo " I am in \$arr The keys in the array are: ".$b['key']; 
12 echo "<br/>"; 
13 echo " I am in \$arr The values in the array are: ".$b['value']; 
14 ?> 

Is shown as:

When we use 0,1 as the index:
My key in the $arr array is: 0
My value in the $arr array is: I am the first value
When we use key,value as the index:

My key in the $arr array is: 1
My value in the $arr array is: I am the second value

5. Use the combination of each function and list function to traverse the number group, 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. Loop through the array using for

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 elements: '.$array15[$i].'<br>'; 
6 } 
7 ?> 

The output result is:

Array element: a
Array element: b
Array element: c
Array element: d
Array element: e
Array element: f

Related articles: