PHP study notes array

Source: Internet
Author: User
In fact, arrays in PHP are very similar to arrays in JavaScript, and are a set of key-value pairs. I. 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 by assigning values to each element.

Format: $ arrayname [key] = value;

Here, 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 numbers 0, 1, 2, 3, or a string. As follows:

The code is as follows:


1 2 // use the values 1, 2, 3 as the key 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, the default key of the array is a value that increases progressively 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 // use a 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)
The key value of array $ array3 is:

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

(2) use the array function to directly define an array.

Format: $ arrayname = array (key1 => value1, key2 => value2 );

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

For example, the following code:

The code is as follows:


1 2 // use a value as the key
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 a string as the 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 // key omitted
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 and values of array $ array8 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 the element is the auto-increment non-repeated value of the specified value.

It is difficult to understand the literal meaning. let's look at an example:

Run the following code:

The code is as follows:


1 2 // The Key display of the first element of array $ array4 is specified as 2, and then the 2nd and 3 elements are omitted as keys.
3 $ array4 [2] = 'a ';
4 $ array4 [] = 'B ';
5 $ array4 [] = 'C ';
6 // The Key display of the 4th elements is specified as 10, and the 5th and 6 elements later are omitted.
7 $ array4 [10] = 'd ';
8 $ array4 [] = 'e ';
9 $ array4 [] = 'F ';
10 // The Key display of 7th elements is specified as 9, and the subsequent 8th and 9 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] => h [14] => I)

Note: the key of the seventh element is 9. Normally, the value of the eighth element is 10, but the key has been used before 10, 11, and 12, the key of the eighth element is 13.

2> whether it is a number or a string as the key of an array element, it represents only the key of this element, and there is no direct relationship with the position of this element in the array, this is the biggest difference from arrays in C # and other languages. The following is an example.

Run the following code:

The code is as follows:


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

$ Array5 [0] is empty!

';
6}
7?>


The result is:

$ Array5 [0] is empty!

Note: $ array5 [0] represents the value of the element with the value 0 in the middle of the array (not like C # and other languages that represent the first element of the array ), because the array only has the key as the string 'one', and the key without the element is 0, $ 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. When creating an array, you can mix numbers and strings as the key of the element. The code is as follows:

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 the keys of arrays, as shown below:

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 array elements

1. General method

To obtain an element in an array, you only need to add a key to the array name with brackets. the call method is as follows:

$ Arrayname [key];

2. use foreach results to traverse the array

To access each array element, you can use the foreach loop:

Foreach ($ array as $ value)

{

// Do something with $ value

}

The Foreach loop iterates every element in the array $ array and assigns the value of each element to the $ value variable. The following is an example:

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 value of array $ array11 is a, B, c, d, e,



Using foreach, you can also 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

}

Here, $ key is the key of each element and the value of $ value. The following code demonstrates how to create a drop-down box using the foreach structure:

The code is as follows:


1 2 $ array12 = array ('one' => 1, 'two' => 2, 'Three '=> 3, 'Four' => 4, 'Five' => 5 );
3 echo'';4 foreach ($ array12 as $ key => $ value)5 {6 echo"$ Key";7}8 echo'';
9?>



3. use the list function to access the array

The List function assigns values in the array to some variables. its function syntax is as follows:

Void list (mixed varname, mixed varname2 ......)

See the following example:

The code is as follows:


1 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'
';
7 // assign a value to some variables
8 list ($ flag2, $ grassland2) = $ array13;
9 echo "$ flag2 $ grassland2 ";
10 echo'
';
11 // assign only the value to the third variable
12 list (, $ grassland3) = $ array13;
13 echo "$ grassland3 ";
14 echo'
';
15?>


Output result:

Red blue green
Red green
Green

Note: list () can only be used as an array of numeric indexes and the number index must start from 0.

Because the list function first assigns the element value of the input key to 0 to the first variable, then assigns the element value of the input key to the second variable, and so on, therefore, the number and position of variables in the list function must correspond to the number key in the array to obtain the desired value. In addition, the list function cannot access the array element with the string as the key. As follows:

The code is as follows:


1 2 $ array13 = array (1 => 'red', 'blue', 'green ');
3 list ($ flag1, $ sky1, $ grassland1) = $ array13;
4 echo '$ flag1 value:'. $ flag1 .'
';
5 echo '$ sky1 value:'. $ sky1 .'
';
6 echo '$ grassland1 value:'. $ grassland1 .'
';
7?>


The output result is:

The value of $ flag1 is:
$ Sky1: red
$ Grassland1 value: blue

Note: the value of $ flag1 should be the element value of 0 in the array, but the first element of this array is the key 1, and there is no element with the key 0, therefore, the value of $ flag1 is empty, which also changes the values of $ sky1 and $ grassland1.

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 one step forward. Note that it is a pair, which is described in detail below. Syntax of this function:

Array each (array & $ array)

Returns the key/value pair of the current pointer position in the array and moves the array pointer forward. Each key-value pair is returned as an array of four units. The key value is 0, 1, and the key and value elements. Element 0 and key contain the key names of the array units, and 1 and value contain data. If the internal pointer crosses the end of the array, each () returns FALSE. Why are there four tables in the each function? In fact, the each function obtains these four subscripts for our convenience. we can use 0, 1 as the index, or key and value as the index. See the following code:

The code is as follows:


1 2 $ arr = array ("I am the first value", "I am the second value", "I am the third value ");
3 echo "when we use 0, 1 as the index:

";
4 $ a = each ($ arr );
5 echo "my keys in the \ $ arr array are:". $ a ['0'];
6 echo"
";
7 echo "my value in the \ $ arr array is:". $ a ['1'];
8 echo"

";
9 echo "when we use the key and value as the index:

";
10 $ B = each ($ arr );
11 echo "my keys in the \ $ arr array are:". $ B ['key'];
12 echo"
";
13 echo "my value in the \ $ arr array is:". $ B ['value'];
14?>


Shown:

When we use 0, 1 as the index:
The key in my $ arr array is: 0
My value in the $ arr array is: I am the first value.
When key and value are used as indexes:

My keys in the $ arr array are: 1
My value in the $ arr array is: I am the second value.

5. use the each function and the list function to traverse the array, as shown in the following example:

The code is as follows:


1 2 $ array14 = array ('a' => 'apple', 'B' => 'bana', 'C' => 'crancel ');
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 the for loop to access the array

For example:

The code is as follows:


1 2 $ array15 = array ('A', 'B', 'C', 'D', 'e', 'F ');
3 for ($ I = 0; $ I 4 {
5 echo 'Array element: '. $ array15 [$ I].'
';
6}
7?>


Output result:

Array element:
Array element: B
Array element: c
Array element: d
Array element: e
Array element: f

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.