Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
// 1.array_count_values() 返回一个数组:数组的键是 array 里单元的值;数组的值是 array 单元的值出现的次数。
//返回一个关联数组,用 array 数组中的值作为键名,该值在数组中出现的次数作为值。
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
//count — 计算数组中的单元数目,或对象中的属性个数
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump(count($a));
//合并
//array_merge( array $... = ?) : array 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组
//注意 键名相同后面会覆盖前面的值
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
//array_intersect( array $array1, array $array2 , array $... = ?) : array 返回一个数组,该数组包含了所有在 array1 中也同时出现在所有其它参数数组中的值。注意键名保留不变
//array1 要检查的数组,作为主值。 array2 要被对比的数组。
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
// array_unique(数组名,排序规则) — 移除数组中重复的值
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
//运算
//array_sum() 将数组中的所有值相加,并返回结果。
$a = array(2, 4, 6, 8);
echo array_sum(($b));
//array_product() 以整数或浮点数返回一个数组中所有值的乘积。
$a = array(2, 4, 6, 8);
echo array_product($a);
//排序
// arsort(数组名,排序规则);对数组进行逆向排序并保持索引关系
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
//asort — 对数组进行排序并保持索引关系
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
//krsort — 对数组按照键名逆向排序
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
//ksort — 对数组按照键名排序
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
//rsort — 对数组逆向排序
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
//sort — 对数组排序
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}