An array of PHP learning notes

Source: Internet
Author: User
Tags foreach format define array empty numeric value variables variable
How to define an array: There are two main ways to create an array in PHP, so let's look at how to create an array

(1) Create an array of methods for assigning values directly to each element.

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

Where Arrayname is the name of the array, key is the element of the array, and value is the element. The key can be a 0,1,2,3 or a string. As shown below:
Copy CodeThe code is as follows:
1 <?php
2//1,2,3 value as the key of the array
3 The key value of Echo ' <p> array $array1 is:</p> ';
4 $array 1[1]= ' a ';
5 $array 1[2]= ' B ';
6 $array 1[3]= ' C ';
7 Print_r ($array 1);
8
9//If the key is omitted, the default key for the array is the value that is incremented starting from 0
The key value of Echo ' <p> array $array2 is:</p> ';
One $array 2[]= ' a ';
$array 2[]= ' B ';
$array 2[]= ' C ';
Print_r ($array 2);
15
16//Key with string as array
The key value of Echo ' <p> array $array3 is:</p> ';
$array 3[' One ']= ' a ';
$array 3[' two ']= ' B ';
$array 3[' three ']= ' C ';
Print_r ($array 3);
?>

The results of the above code output are:

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 [two] => b [three] => c)

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

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

Where Arrayname is the array name, Key1, key2 the array key, value1, value2 correspond to the Key1 and Key2 values respectively.

Give an example of the following code:
Copy CodeThe code is as follows:
1 <?php
2//With numeric value as key
3 $array 6=array (1=> ' A ',2=> ' B ',3=> ' C ');
4 the key and value of Echo ' <p> array $array6 are:</p> ';
5 Print_r ($array 6);
6//with string as key
7 $array 7=array (' One ' => ' a ', ' two ' => ' B ', ' Three ' => ' C ');
8 the key and value of Echo ' <p> array $array7 are:</p> ';
9 Print_r ($array 7);
10//Ellipsis key to the wording
One $array 8=array (' A ', ' B ', ' C ');
The keys and values of the echo ' <p> array $array8 are:</p> ';
Print_r ($array 8);
?>

The results are:

The keys and values for the array $array6 are:

Array ([1] => a [2] => b [3] => C)
The keys and values for the array $array7 are:

Array ([one] => a [two] => b [three] => c)
The keys and values for 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, then all elements after this element, whose default key is the value of the specified value, are not duplicates.

It's a little hard to understand the literal meaning, let's take a look at an example:

The following code:
Copy CodeThe code is as follows:
1 <?php
2//array $array4 The key display of the first element is specified as 2, after which the 2nd and 3 elements are known to omit the key
3 $array 4[2]= ' a ';
4 $array 4[]= ' B ';
5 $array 4[]= ' C ';
6//4th Elements of the key display specified as 10, after the 5th, 6 yuan is the way to omit the key
7 $array 4[10]= ' d ';
8 $array 4[]= ' e ';
9 $array 4[]= ' F ';
10//7th elements of the key display specified as 9, after the 8th, 9 yuan is the way to omit the key
One $array 4[9]= ' G ';
$array 4[]= ' h ';
$array 4[]= ' i ';
14//print array keys and values
Print_r ($array 4);
?>

The results are:

Array ([2] => a [3] => b [4] => C [ten] => d [one] => e [[] => F [9] => g [[] => H [] => i)

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

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

The following code:
Copy CodeThe code is as follows:
1 <?php
2 $array 5[' one ']= ' a ';
3 if (!isset ($array 5[0])
4 {
5 echo ' <p> $array 5[0] is empty! </p> ';
6}
7?>

The results are:

$array 5[0] is empty!

Description: $array 5[0] Represents the value of an element with a key value of 0 in an array (unlike the first element of the array in languages such as C #), because the array has only the key of the string ' One ' and the key of 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 a key and a string as a key. You can mix numbers and strings as keys for elements when creating arrays. As shown in the following code:
Copy CodeThe code is as follows:
1 <?php
2 $array 9=array (1=> ' A ', 2=> ' B ', ' One ' => ' C ', ' two ' => ' d ', ' e ', ' f ', ' G ');
3 the key and value of Echo ' <p> array $array9 are:</p> ';
4 Print_r ($array 9);
5?>

The results are:

The keys and values for the 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 the key of an array, as follows:
Copy CodeThe code is as follows:
1 <?php
2 $key 1= ' one ';
3 $key 2= ' two ';
4 $key 3= ' three ';
5 $array 10[$key 1]= ' a ';
6 $array 10[$key 2]= ' B ';
7 $array 10[$key 3]= ' C ';
8 the key and value of Echo ' <p> array $array10 are:</p> ';
9 Print_r ($array 10);
Ten?>

The results are:

The keys and values for the array $array10 are:

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

Second, how to access the elements of an array

1. General methods

To get an element in an array, just use the array name plus the brackets and a key to invoke the method as follows:

$arrayname [key];

2. Traverse the 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 will iterate over each element in the group $array and assign the value of each element to the $value variable, for example:
Copy CodeThe code is as follows:
1 <?php
2 $array 11=array (' A ', ' B ', ' C ', ' d ', ' e ');
3 The value of Echo ' <p> array $array11 is: ';
4 foreach ($array as $value)
5 {
6 echo $value. ', ';
7}
8 echo ' </p> ';
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 <?php
2 $array 12=array (' One ' =>1, ' two ' =>2, ' three ' =>3, ' four ' =>4, ' five ' =>5);
3 echo ' <select name= ' onetofive ' > ';
4 foreach ($array as $key => $value)
5 {
6 echo "<option value=\" $value \ "> $key </option>";
7}
8 echo ' </select> ';
9?>


3, using the list function to access the array

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

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

See the following example:
Copy CodeThe code is as follows:
1 <?php
2 $array 13=array (' Red ', ' blue ', ' green ');
3//Assign value to all variables
4 List ($flag 1, $sky 1, $grassland 1) = $array 13;
5 echo "$flag 1 $sky 1 $grassland 1";
6 echo ' <br> ';
7//Assign to partial variable
8 List ($flag 2,, $grassland 2) = $array 13;
9 echo "$flag 2 $grassland 2";
Echo ' <br> ';
11//Only assign a value to the third variable
List (,, $grassland 3) = $array 13;
echo "$grassland 3";
Echo ' <br> ';
?>

The output results are:

Red Blue Green
Red Green
Green

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

Because the list function first assigns the value of the element in the array to the first variable, which is the key of 0, then assign the value of the element of 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 number keys in the array to get the desired value. And the list function is not accessing an array element with a string as its key. As shown below:
Copy CodeThe code is as follows:
1 <?php
2 $array 13=array (1=> ' red ', ' blue ', ' green ');
3 List ($flag 1, $sky 1, $grassland 1) = $array 13;
The value of 4 echo ' $flag 1 is: '. $flag 1. ' <br> ';
The value of 5 echo ' $sky 1 is: '. $sky 1. ' <br> ';
The value of 6 echo ' $grassland 1 is: '. $grassland 1. ' <br> ';
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 the $FLAG1 is supposed to be an element value with a key of 0 in the array, but the first element is a key with 1, and no key is 0, the value of $flag1 is empty and therefore the value of the $sky1 and $grassland1 behind is changed.

4, use each function to access the array

The each function is to return the current key/value pair in the array and move the array pointer one step forward, note that a pair is described in detail below. The function syntax:

Array each (array & $array)

Returns the key/value pair of the current pointer position in array arrays 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 names of the array cells, and 1 and value contain data. Each () returns FALSE if the internal pointer crosses the end of the array. Why are there four tables in each function? In fact, each function to get these four subscript is only convenient for us to operate, we can use 0,1 as an index, can also use Key,value as an index. Please see the following code:
Copy 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,1 as index:<br/><br/>";
4 $a =each ($arr);
5 echo "My key in \ $arr array is:". $a [' 0 '];
6 echo "<br/>";
7 echo "I am in \ $arr The value in the array is:". $a [' 1 '];
8 echo "<br/><br/>";
9 echo "When we use Key,value as index:<br/><br/>";
$b =each ($arr);
One echo "I'm in \ $arr the key in the array is:". $b [' key '];
echo "<br/>";
echo "I am in \ $arr The value in the array is:". $b [' value '];
?>

Display as:

When we use 0,1 as an index:
My keys in the $arr array are: 0
My values in the $arr array are: I'm 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 am the second value

5, using each function and the list function to traverse the array, the following example:
Copy CodeThe code is as follows:
1 <?php
2 $array 14=array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' cranberry ');
3 while (the list ($key, $value) = each ($array 14))
4 {
5 echo "$key => $value \ n";
6}
7?>

The output is:

A => Apple B => Banana C => Cranberry

6. Use for loop to access array

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

The output results are:

Array element: A
Array elements: b
Array elements: C
Array elements: 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.