PHP array:
An array contains a set of keywords and values. values can be of any type. See the following simple example:
[Php]
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> array entry </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? Php
$ Hens [0] = 3;
$ Hens [1] = 5;
$ Hens [2] = 2.5;
$ Hens [3] = 4;
$ Hens [4] = 3.5;
$ Hens [5] = 17;
// To calculate the number of array elements, use the system function count;
Echo "Total". count ($ hens). "Chicken <br/> ";
$ Allwight = 0;
For ($ I = 0; $ I <count ($ hens); $ I ++ ){
Echo "no.". $ I. "chicken weight". $ hens [$ I]. "<br/> ";
$ Allwight + = $ hens [$ I];
}
Echo "Maximum Weight:". $ allwight. "; average weight:". $ allwight/count ($ hens );
?>
</Body>
</Html>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> array entry </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? Php
$ Hens [0] = 3;
$ Hens [1] = 5;
$ Hens [2] = 2.5;
$ Hens [3] = 4;
$ Hens [4] = 3.5;
$ Hens [5] = 17;
// To calculate the number of array elements, use the system function count;
Echo "Total". count ($ hens). "Chicken <br/> ";
$ Allwight = 0;
For ($ I = 0; $ I <count ($ hens); $ I ++ ){
Echo "no.". $ I. "chicken weight". $ hens [$ I]. "<br/> ";
$ Allwight + = $ hens [$ I];
}
Echo "Maximum Weight:". $ allwight. "; average weight:". $ allwight/count ($ hens );
?>
</Body>
</Html> [plain] view plaincopyprint?
A total of six chickens
0th chicken weight 3
1st chicken weight 5
2nd chickens weigh 2.5
3rd chicken weight 4
4th chickens weigh 3.5
5th chicken weight 17
Maximum weight: 35; average weight: 5.8333333333333
A total of six chickens
0th chicken weight 3
1st chicken weight 5
2nd chickens weigh 2.5
3rd chicken weight 4
4th chickens weigh 3.5
5th chicken weight 17
Maximum weight: 35; average weight: 5.8333333333333 create an array
There are many ways to create arrays. The above example is the most common method.
In the PHP array, the value of each element can be of any type!
Method 2:
$ Arr = array (1,90, "hello", null );
Method 3: Create an array
$ Arr ["logo"] = "beijing"; $ arr ["MK"] = 124; $ arr [4] = 678;
It is equivalent to the following:
$ Arr = array ("logo" => "beijing", "MK" => 123,4 => 678 );
Traversal method:
Foreach ($ arr as $ key => $ val ){
Echo $ key. "=". $ var. "<br/> ";
}
The third method is to specify that the array cannot be accessed through the for loop. The above method should be used for traversal.
[Php]
// Array considerations:
// When we create an array, if a subscript is not specified for an element, PHP will automatically use the largest value (integer) and Add 1 as the subscript of the element.
$ Arr = array (5 => "logo", 55,56 );
$ Arr = array (5 => "logo", 6 => 55,7 => 56 );
// Array considerations:
// When we create an array, if a subscript is not specified for an element, PHP will automatically use the largest value (integer) and Add 1 as the subscript of the element.
$ Arr = array (5 => "logo", 55,56 );
$ Arr = array (5 => "logo", 6 => 55,7 => 56 );
Array applications
[Php]
<? Php
// Create a simple array
$ Array = array (1, 2, 3, 4, 5 );
Print_r ($ array );
Echo "<br/> ";
// Delete All cells, but keep the structure of the array.
Foreach ($ array as $ I =>$ value ){
Unset ($ array [$ I]);
}
Print_r ($ array );
Echo "<br/> ";
// Add a unit (note that the new key name is 5, rather than the value 0 you may think)
$ Array [] = 6;
Print_r ($ array );
Echo "<br/> ";
// Re-index:
$ Array = array_values ($ array );
$ Array [] = 7;
Print_r ($ array );
Echo "<br/> ";
Var_dump ($ array );
?>
<? Php
// Create a simple array
$ Array = array (1, 2, 3, 4, 5 );
Print_r ($ array );
Echo "<br/> ";
// Delete All cells, but keep the structure of the array.
Foreach ($ array as $ I =>$ value ){
Unset ($ array [$ I]);
}
Print_r ($ array );
Echo "<br/> ";
// Add a unit (note that the new key name is 5, rather than the value 0 you may think)
$ Array [] = 6;
Print_r ($ array );
Echo "<br/> ";
// Re-index:
$ Array = array_values ($ array );
$ Array [] = 7;
Print_r ($ array );
Echo "<br/> ";
Var_dump ($ array );
?> [Plain]
Execution result:
Execution result: [plain]
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)
Array ()
Array ([5] => 6)
Array ([0] => 6 [1] => 7)
Array (2) {[0] => int (6) [1] => int (7 )}
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)
Array ()
Array ([5] => 6)
Array ([0] => 6 [1] => 7)
Array (2) {[0] => int (6) [1] => int (7 )}
Common PHP Functions
1. count
Count the number of array elements, using: count ($ arr );
2. is-array
Determine whether it is an array, is_array ($ arr );
3. print_r and var_dump (the array element type can be displayed)
Display array information
4. explode (used in actual development)
Splits string functions. Explode ("What to split", string)
5. foreach
Array traversal function:
[Plain]
Foreach ($ arr as $ key => $ val ){
Echo $ key. "=". $ var. "<br/> ";
}
Foreach ($ arr as $ key => $ val ){
Echo $ key. "=". $ var. "<br/> ";
} 6. unset
Unset () deletes an element in the array, but does not reorganize the index. The previous example has already been described!
7. array_values
Re-indexing. It is generally used with the above function! Example
Array comparison
[Php
<? Php
$ A = array ("a" => "apple", "B" => "banana ");
$ B = array ("a" => "pear", "B" => "strawberry", "c" => "cherry ");
$ C = $ a + $ B; // Union of $ a and $ B
Echo "Union of \ $ a and \ $ B: \ n ";
Var_dump ($ c );
$ C = $ B + $ a; // Union of $ B and $
Echo "Union of \ $ B and \ $ a: \ n ";
Var_dump ($ c );
?>
<? Php
$ A = array ("a" => "apple", "B" => "banana ");
$ B = array ("a" => "pear", "B" => "strawberry", "c" => "cherry ");
$ C = $ a + $ B; // Union of $ a and $ B
Echo "Union of \ $ a and \ $ B: \ n ";
Var_dump ($ c );
$ C = $ B + $ a; // Union of $ B and $
Echo "Union of \ $ B and \ $ a: \ n ";
Var_dump ($ c );
?> [Plain]
Union of $ a and $ B:
Array (3 ){
["A"] =>
String (5) "apple"
["B"] =>
String (6) "banana"
["C"] =>
String (6) "cherry"
}
Union of $ B and $:
Array (3 ){
["A"] =>
String (4) "pear"
["B"] =>
String (10) "strawberry"
["C"] =>
String (6) "cherry"
}
Union of $ a and $ B:
Array (3 ){
["A"] =>
String (5) "apple"
["B"] =>
String (6) "banana"
["C"] =>
String (6) "cherry"
}
Union of $ B and $:
Array (3 ){
["A"] =>
String (4) "pear"
["B"] =>
String (10) "strawberry"
["C"] =>
String (6) "cherry"
}
[Php]
Comparison Array
<? Php
$ A = array ("apple", "banana ");
$ B = array (1 => "banana", "0" => "apple ");
Var_dump ($ a = $ B); // bool (true)
Var_dump ($ a ===$ B); // bool (false)
?>
Comparison Array
<? Php
$ A = array ("apple", "banana ");
$ B = array (1 => "banana", "0" => "apple ");
Var_dump ($ a = $ B); // bool (true)
Var_dump ($ a ===$ B); // bool (false)
?>
[Php]
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> comprehensive case </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? Php
// Error_reporting (E_ALL ^ E_NOTICE );
$ Aar = $ _ REQUEST ["grade"];
$ Arr = explode (",", $ aar );
$ Allval = 0;
Foreach ($ arr as $ val ){
$ Allval + = $ val;
}
?>
<H1> enter the score of the trainee, separated by commas
<Form action = "array03.php" method = "post">
<Input type = "text" name = "grade" value = "<? Php echo $ aar?> "/>
<Input type = "submit" value = "calculate average"/>
</Form>
<? Php
$ Aar = $ _ REQUEST ["grade"];
$ Arr = explode (",", $ aar );
$ Allval = 0;
Foreach ($ arr as $ val ){
$ Allval + = $ val;
}
?>
<? Php
Echo "average value:". round ($ allval/count ($ arr), 2 );
?>
</Body>
</Html>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> comprehensive case </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? Php
// Error_reporting (E_ALL ^ E_NOTICE );
$ Aar = $ _ REQUEST ["grade"];
$ Arr = explode (",", $ aar );
$ Allval = 0;
Foreach ($ arr as $ val ){
$ Allval + = $ val;
}
?>
<H1> enter the score of the trainee, separated by commas
<Form action = "array03.php" method = "post">
<Input type = "text" name = "grade" value = "<? Php echo $ aar?> "/>
<Input type = "submit" value = "calculate average"/>
</Form>
<? Php
$ Aar = $ _ REQUEST ["grade"];
$ Arr = explode (",", $ aar );
$ Allval = 0;
Foreach ($ arr as $ val ){
$ Allval + = $ val;
}
?>
<? Php
Echo "average value:". round ($ allval/count ($ arr), 2 );
?>
</Body>
</Html>