Home > Backend Development > PHP Tutorial > PHP Study Notes: Array_PHP Tutorial

PHP Study Notes: Array_PHP Tutorial

WBOY
Release: 2016-07-21 15:27:36
Original
843 people have browsed it

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 directly assigning a value to each element.

The format is: $arrayname[key]=value;

where arrayname is the name of the array, key is the key of the array element, and value is the value of the element. The key can be a number such as 0, 1, 2, or 3, or a string. As shown below:

Copy code The code is as follows:

1 2 //Use 1, The values ​​​​of 2 and 3 are used as the keys of the array
3 echo '

The key value of array $array1 is:

';
4 $array1[1]='a';
5 $array1[2]='b';
6 $array1[3]='c';
7 print_r($array1);
8
9 //If the key is omitted method, the default key of the array is a value that increases from 0
10 echo '

The key value of array $array2 is:

';
11 $array2[]='a ';
12 $array2[]='b';
13 $array2[]='c';
14 print_r($array2);
15
16 //With characters String as the key of the array
17 echo '

The key value of array $array3 is:

';
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 )
Array The key value of $array3 is:

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

(2) Use the array function directly Define an array.

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, and value1 and value2 correspond respectively. The values ​​of key1 and key2.

Give an example, such as the following code:
Copy code The code is as follows:

1 2 //Use numerical values ​​as keys
3 $array6=array(1=>'a',2=>'b',3=>'c');
4 echo '

The keys and values ​​of array $array6 are:

';
5 print_r($array6);
6 //Use string as key
7 $array7=array ('one'=>'a','two'=>'b','three'=>'c');
8 echo '

The keys and values ​​of array $array7 are :

';
9 print_r($array7);
10 //How to omit the key
11 $array8=array('a','b','c');
12 echo '

The keys and values ​​of array $array8 are:

';
13 print_r($array8);
14 ?>

The result is:

The keys and values ​​of array $array6 are:

Array ( [1] => a [2] => b [3] => c )
The keys and values ​​of array $array7 are:

Array ( [one] => a [two] => b [three] => c )
The keys of array $array8 The keys and values ​​are:

Array ( [0] => a [1] => b [2] => c )

Note:

1 >If you specify a value as the key for an element in the array, the default key of all elements after this element is the self-increasing, non-repeating 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 code The code is as follows:

1 2 //The key of the first element of array $array4 is specified as 2, and the keys of the subsequent 2nd and 3rd elements are omitted. Method
3 $array4[2]='a';
4 $array4[]='b';
5 $array4[]='c';
6 //The 4th The key of the element is explicitly specified as 10, and the keys of the subsequent 5th and 6th elements are omitted
7 $array4[10]='d';
8 $array4[]='e';
9 $array4[]='f';
10 //The key of the 7th element is specified as 9, and the keys of the subsequent 8th and 9th elements are omitted
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] => ス [14] => ス )

Explanation: The key of the seventh element is 9. Under normal circumstances, the key The eight elements should be 10, but keys 10, 11 and 12 have been used by elements before, so the key of the eighth element is 13.

2> Whether a number or a string is used as the key of an array element, it only represents the key of this element and has no direct relationship with the position of this element in the array. This is related to C# The biggest difference between arrays in other languages. Here's an example.

The following code:
Copy the code The code is as follows:

1 2 $array5['one']='a';
3 if(!isset($array5[0]))
4 {
5 echo '

$array5[0] is empty of!

';
6 }
7 ?>

The result is:

$array5[0] is empty!

Explanation: $array5[0] represents the value of the element whose key is 0 in the array (it does not represent the first element of the array like C# and other languages), because the array only has keys that are characters For the element string 'one', there is no element with a key of 0, so $array5[0] is empty.

3>PHP supports two types of arrays: indexed array and associative array. The former uses numbers as keys, and the latter uses strings as keys. You can use a mix of numbers and strings as keys for elements when creating an array. The code is as follows:
Copy code The code is as follows:

1 2 $array9=array (1=>'a', 2=>'b', 'one'=>'c', 'two'=>'d', 'e', ​​'f', 'g');
3 echo '

The keys and values ​​of array $array9 are:

';
4 print_r($array9);
5 ?>

The result is:

The keys and values ​​of 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 of arrays, as shown below:
Copy code The code is as follows:

1 2 $key1='one';
3 $key2='two';
4 $key3='three';
5 $array10[$key1]='a';
6 $array10[$key2]='b';
7 $array10[$key3]='c';
8 echo '

The keys and values ​​of array $array10 are:

';
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 the elements of the array

1. General method

To get an element in the array elements, just use the array name plus brackets and a key. The calling method is as follows:

$arrayname[key];

2. Use the foreach result 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 each element in the array $array and assign the value of each element to the $value variable. Here is an example:
Copy code The code is as follows:

1 2 $array11=array('a','b',' c','d','e');
3 echo '

The value of array $array11 is:';
4 foreach($array11 as $value)
5 {
6 echo $value.',';
7 }
8 echo '

';
9 ?>

The output result is:

The values ​​of array $array11 are: a,b,c,d,e,



Using foreach, you can also access the keys and values ​​of the 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 $value is the value of the element. The following code demonstrates how to use the foreach structure to create a drop-down box:
Copy code The code is as follows:

1 2 $array12=array('one'=>1,'two'=>2,'three'= >3,'four'=>4,'five'=>5);
3 echo '