PHP _php tutorial for an array of learning notes

Source: Internet
Author: User
How to define an array: There are two main ways to create an array in PHP, let's look at how to create an array

(1) Create an array of methods that assign values directly to each element.

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

Where Arrayname is the name of the array, the key is the element of the array, and value is the values of the elements. The key can be a 0,1,2,3, or it can be a string. As shown below:
Copy CodeThe code is as follows:
1 2//Use the numeric value of the array as the key
3 Echo '

The key values for the array $array1 are:

';
4 $array 1[1]= ' a ';
5 $array 1[2]= ' B ';
6 $array 1[3]= ' C ';
7 Print_r ($array 1);
8
9//If you omit the key, the default key for the array is the numeric value that increments from 0
Ten Echoes '

The key values for the array $array2 are:

';
One $array 2[]= ' a ';
$array 2[]= ' B ';
$array 2[]= ' C ';
Print_r ($array 2);
15
16//String as the key to the array
Echo '

The key values for the array $array3 are:

';
$array 3[' One ']= ' a ';
$array 3[']= ' B ';
$array 3[' three ']= ' C ';
Print_r ($array 3);
?>

The output of the above code results in:

The key values for the array $array1 are:

Array ([1] = a [2] = b [3] = = c)
The key values for the array $array2 are:

Array ([0] = a [1] = b [2] = = c)
The key values for the array $array3 are:

Array ([one] = a [b] = [three] + c)

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

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

Where Arrayname is an array name, Key1, Key2 are the keys for the array, value1, value2 correspond to Key1 and Key2 values respectively.

As an example, the following code:
Copy CodeThe code is as follows:
1 2//value as the key
3 $array 6=array (1=> ' A ',2=> ' B ',3=> ' C ');
4 Echo '

The keys and values of the array $array6 are:

';
5 Print_r ($array 6);
6//String as the key
7 $array 7=array (' one ' = ' a ', ' one ' and ' B ', ' three ' = ' C ');
8 Echo '

The keys and values of the array $array7 are:

';
9 Print_r ($array 7);
10//Ellipsis key notation
One $array 8=array (' A ', ' B ', ' C ');
Echo '

The keys and values of the array $array8 are:

';
Print_r ($array 8);
?>

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 [b] = [three] + c)
The keys and values of the array $array8 are:

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

Attention:

1> If you specify a numeric value as its key for an element in an array, all elements after this element whose default key is the self-increment non-repeating value of the specified value.

Simply looking at the literal meaning is a bit difficult to understand, let's take a look at an example:

The following code:
Copy CodeThe code is as follows:
1 2//array $array4 The key display of the first element is specified as 2, followed by the 2nd, 3 pixels omitting the key way
3 $array 4[2]= ' a ';
4 $array 4[]= ' B ';
5 $array 4[]= ' C ';
6//4th element key display specified as 10, followed by the 5th, 6 pixels omitted the key way
7 $array 4[10]= ' d ';
8 $array 4[]= ' e ';
9 $array 4[]= ' F ';
10//7th element key display specified as 9, followed by the 8th, 9 pixels omitted the key way
One $array 4[9]= ' G ';
$array 4[]= ' h ';
$array 4[]= ' i ';
14//Print the keys and values of an array
Print_r ($array 4);
?>

The result is:

Array ([2] = a [3] = b [4] = + C [ten] ~ D [one] ~ E [[j] = f [9] = g [ALL] = h [+] = i)

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

2> whether a key is a number or a string as an array element, it represents only the key of this element, which is not directly related to the position of this element in the array, which is the largest difference from the array in languages such as C #. Let's give an example.

The following code:
Copy CodeThe code is as follows:
1 2 $array 5[' one ']= ' a ';
3 if (!isset ($array 5[0))
4 {
5 Echo '

$array 5[0] is empty!

';
6}
7?>

The result is:

$array 5[0] is empty!

Description: $array 5[0] Represents the value of the element in the array with the key number 0 (not the first element of the array, as in C #), because the array has only the key as the string ' One ', the key for the element is 0, so $array5[0] is empty.

3>php supports two arrays: an indexed array (indexed array) and a union array (associative array), which uses a number as the key, which uses a string as the key. You can mix numbers and strings as keys to elements when you create an array. The code is as follows:
Copy CodeThe code is as follows:
1 2 $array 9=array (1=> ' A ', 2=> ' B ', ' one ' = ' C ', ' one ' = ' d ', ' e ', ' f ', ' G ');
3 Echo '

The keys and values of the array $array9 are:

';
4 Print_r ($array 9);
5?>

The result is:

The keys and values of the array $array9 are:

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

The 4> variable can also be used as the key for the array, as follows:
Copy CodeThe code is as follows:
1 2 $key 1= ' one ';
3 $key 2= ';
4 $key 3= ' three ';
5 $array 10[$key 1]= ' a ';
6 $array 10[$key 2]= ' B ';
7 $array 10[$key 3]= ' C ';
8 Echo '

The keys and values of the array $array10 are:

';
9 Print_r ($array 10);
Ten?>

The result is:

The keys and values of the array $array10 are:

Array ([one] = a [b] = [three] + c)

Second, how to access the elements of the array

1. General methods

To get an element in an array, simply add a key using the array name plus brackets, and the method is called as follows:

$arrayname [key];

2. Iterating through an array using the foreach result

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 iterates through each element in the algebraic group $array and assigns the value of each element to the $value variable, as an example:
Copy CodeThe code is as follows:
1 2 $array 11=array (' A ', ' B ', ' C ', ' d ', ' e ');
3 Echo '

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

';
9?>

The output is:

The value of the array $array11 is: A,b,c,d,e,



You can also use foreach to access the keys and values of an array element at the same time, using:

Foreach ($array as $key = $value)

{

Do something with $key and $value

}

Where $key is the key for each element, $value the value of the element, the following code shows how to create a drop-down box using the foreach structure:
Copy CodeThe code is as follows:
1 2 $array 12=array (' One ' =>1, ' one ' =>2, ' three ' =>3, ' four ' =>4, ' five ' =>5);
3 Echo ' '; 4 foreach ($array $key = + $value) 5 {6 echo "$key "; 7} 8 Echo '';
9?>


3. Using the list function to access the array

The list function assigns the values in the array to some variables whose function syntax is as follows:

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

See the example below:
Copy CodeThe code is as follows:
1 2 $array 13=array (' Red ', ' blue ', ' green ');
3//Assign to all variables
4 List ($flag 1, $sky 1, $grassland 1) = $array 13;
5 echo "$flag 1 $sky 1 $grassland 1";
6 echo '
';
7//Assign to some variables
8 List ($flag 2,, $grassland 2) = $array 13;
9 echo "$flag 2 $grassland 2";
Ten Echoes '
';
11//Assign only to the third variable
List (,, $grassland 3) = $array 13;
echo "$grassland 3";
Echo '
';
?>

The output is:

Red Blue Green
Red Green
Green

Note: List () can only be used for arrays of numeric indexes and the numeric index must start at 0.

Because the list function first assigns the value of the element with the key 0 in the array to the first variable, then assigns the value of the element with the key 1 to the second variable, and so on, so the number and position of the variables in the list function must correspond to the numeric keys in the group to obtain the desired value. The list function is not an array element that accesses a string as a key. As shown below:
Copy CodeThe code is as follows:
1 2 $array 13=array (1=> ' red ', ' blue ', ' green ');
3 List ($flag 1, $sky 1, $grassland 1) = $array 13;
4 Echo ' $flag 1 has a value of: '. $flag 1. '
';
5 echo ' $sky 1 has a value of: '. $sky 1. '
';
6 echo ' $grassland 1 has a value of: '. $grassland 1. '
';
7?>

The output is:

The value of $flag 1 is:
The value of $sky 1 is: red
The value of $grassland 1 is: Blue

Note: Because the value of $FLAG1 should have been an element value of 0 in the array, the first element of this is a key of 1, there is no element with the key 0, so the value of $flag1 is empty, and therefore the values of the subsequent $sky1 and $grassland1 have changed.

4. Use 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, noting that it is a pair, which is explained in detail below. The function syntax:

Array each (array & $array)

Returns the key/value pair of the current pointer position in an array and moves the array pointer forward. Each key-value pair is returned as an array of four cells with a key value of 0,1,key and value four elements. Element 0 and key contain the key name of the array cell, and 1 and value contain the data. If the internal pointer crosses the end of the array, each () returns FALSE. Why does each function have four of the following tables? In fact, each function to get these four subscript is only convenient for us to operate, we can use 0,1 as an index, you can also use Key,value as an index. Take a look at the following code:
Copy CodeThe 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 index:

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

";
9 echo "When we use Key,value as index:

";
$b =each ($arr);
echo "I am in \ $arr The key in the array is:". $b [' key '];
echo "
";
echo "I am in \ $arr The value in the array is:". $b [' value '];
?>

Shown as:

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

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

5. Iterate through the array with each function combined with the list function, as in the following example:
Copy CodeThe code is as follows:
1 2 $array 14=array (' a ' + = ' apple ', ' b ' = ' banana ', ' c ' = ' cranberry ');
3 while (list ($key, $value) = each ($array 14))
4 {
5 echo "$key = $value \ n";
6}
7?>

The output is:

A = apple b = Banana c = Cranberry

6. Using a For loop to access the array

As shown in the following example:
Copy CodeThe code is as follows:
1 2 $array 15=array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ');
3 for ($i =0; $i
4 {
5 echo ' array element: '. $array 15[$i]. '
';
6}
7?>

The output is:

Array elements: A
Array elements: b
Array elements: C
Array elements: D
Array elements: E
Array elements: F

http://www.bkjia.com/PHPjc/323742.html www.bkjia.com true http://www.bkjia.com/PHPjc/323742.html techarticle How to define an array: There are two main ways to create an array in PHP, let's look at how to create an array (1) to create an array directly for each element to assign a value ...

  • 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.