PHP array:
An array is a set of keywords and values, and the values can be of any type; see the following simple introductory case:
[php]
//To count the number of array elements, use the system function count;
echo "The most total are ".count($hens)." chickens
";
$allwight=0;
for($i=0;$i
";
$allwight+=$hens[$i];
}
echo "The maximum weight is: ".$allwight."; the average weight is: ".$allwight/count($hens);
?>
[plain] view plaincopyprint?
There are 6 chickens in total
The 0th chicken weighs 3
The first chicken weighs 5
The second chicken weighs 2.5
The third chicken weighs 4
The 4th chicken weighs 3.5
The 5th chicken weighs 17
The maximum weight is: 35; the average weight is: 5.8333333333333
A total of 6 chickens
The 0th chicken weighs 3
The first chicken weighs 5
The second chicken weighs 2.5
The third chicken weighs 4
The 4th chicken weighs 3.5
The 5th chicken weighs 17
The maximum weight is: 35; the average weight is: 5.8333333333333 Create array
There are many ways to create an array, and the above example is the most common way.
In a PHP array, the value of each element can be of any type!
The second way:
$arr= array(1,90,"hello",null);
The third way to create an array
$arr["logo"] ="beijing";$arr["hsp"]=124;$arr[4]=678;
Equivalent to the following:
$arr=array("logo"=>"beijing","hsp"=>123,4=>678);
Traversal method:
foreach($arr as $key=>$val){
echo $key."=".$var."
";
}
The third method specifies that the array cannot be accessed using a for loop and should be traversed using the above method.
[php]
//Notes on arrays:
//When we create an array, if we do not specify a subscript for an element, PHP will automatically use the current largest subscript value (integer), plus 1 as the subscript of the element.
$arr=array(5=>"logo",55,56);
$arr=array(5=>"logo",6=>55,7=>56);
//Array notes:
//When we create an array, if we do not specify a subscript for an element, PHP will automatically use the current largest subscript value (integer), plus 1 as the subscript of the element.
$arr=array(5=>"logo",55,56);
$arr=array(5=>"logo",6=>55,7=>56);
Some applications of arrays
[php]
//Create a simple array
$array = array(1, 2, 3, 4, 5);
Print_r($array);
echo "
";
// Now delete all cells in it, but keep the structure of the array itself
foreach ($array as $i => $value) {
unset($array[$i]);
}
Print_r($array);
echo "
";
// Add a unit (note that the new key is 5, not 0 as you might think)
$array[] = 6;
print_r($array);
echo "
";
// Reindex:
$array = array_values($array);
$array[] = 7;
print_r($array);
echo "
";
var_dump($array);
?>
//Create a simple array
$array = array(1, 2, 3, 4, 5);
Print_r($array);
echo "
";
// Now delete all cells in it, but keep the structure of the array itself
foreach ($array as $i => $value) {
unset($array[$i]);
}
Print_r($array);
echo "
";
// Add a cell (note that the new key is 5, not 0 as you might think)
$array[] = 6;
print_r($array);
echo "
";
//Reindex:
$array = array_values($array);
$array[] = 7;
print_r($array);
echo "
";
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) }
Commonly used functions related to PHP
1.count
To count the number of array elements, use: count($arr);
2. is-array
Determine whether it is an array, is_array($arr);
3. print_r and var_dump (can display array element types)
Display array information
4. explode (in actual development, it is more useful)
Split string function. explode("split by", string)
5.foreach
Array traversal function:
[plain]
foreach($arr as $key=>$val){
echo $key."=".$var."
";
}
foreach($arr as $key=>$val){
echo $key."=".$var."
";
}6. unset
unset() deletes an element in the array, but does not reorganize the index. The previous example has already explained it!
7. array_values
Re-indexing is generally used in conjunction with the above function! Examples of what to look for specifically
数组比较
[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 $a
echo "Union of $b and $a: n";
var_dump($c);
?>
$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 $a
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 $a:
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 $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
[php]
比较数组
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
比较数组
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
[php]
//error_reporting(E_ALL^E_NOTICE);
$aar=$_REQUEST["grade"];
$arr=explode(",",$aar);
$allval=0;
foreach($arr as $val){
$allval+=$val;
}
?>
$aar=$_REQUEST["grade"];
$arr=explode(",",$aar);
$allval=0;
foreach($arr as $val){
$allval+=$val;
}
?>
echo "Average:".round($allval/count($arr),2);
?>