Blogger Information
Blog 37
fans 0
comment 0
visits 34759
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组函数练习
手机用户1607314868
Original
1018 people have browsed it
统计
  1. // 1.array_count_values() 返回一个数组:数组的键是 array 里单元的值;数组的值是 array 单元的值出现的次数。
  2. //返回一个关联数组,用 array 数组中的值作为键名,该值在数组中出现的次数作为值。
  3. $array = array(1, "hello", 1, "world", "hello");
  4. print_r(array_count_values($array));
  5. //count — 计算数组中的单元数目,或对象中的属性个数
  6. $a[0] = 1;
  7. $a[1] = 3;
  8. $a[2] = 5;
  9. var_dump(count($a));

//合并

  1. //array_merge( array $... = ?) : array 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组
  2. //注意 键名相同后面会覆盖前面的值
  3. $array1 = array("color" => "red", 2, 4);
  4. $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
  5. $result = array_merge($array1, $array2);
  6. print_r($result);
  7. //array_intersect( array $array1, array $array2 , array $... = ?) : array 返回一个数组,该数组包含了所有在 array1 中也同时出现在所有其它参数数组中的值。注意键名保留不变
  8. //array1 要检查的数组,作为主值。 array2 要被对比的数组。
  9. $array1 = array("a" => "green", "red", "blue");
  10. $array2 = array("b" => "green", "yellow", "red");
  11. $result = array_intersect($array1, $array2);
  12. print_r($result);
  13. // array_unique(数组名,排序规则) — 移除数组中重复的值
  14. $input = array("a" => "green", "red", "b" => "green", "blue", "red");
  15. $result = array_unique($input);
  16. print_r($result);

//运算

  1. //array_sum() 将数组中的所有值相加,并返回结果。
  2. $a = array(2, 4, 6, 8);
  3. echo array_sum(($b));
  4. //array_product() 以整数或浮点数返回一个数组中所有值的乘积。
  5. $a = array(2, 4, 6, 8);
  6. echo array_product($a);

//排序

  1. // arsort(数组名,排序规则);对数组进行逆向排序并保持索引关系
  2. $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
  3. arsort($fruits);
  4. foreach ($fruits as $key => $val) {
  5. echo "$key = $val\n";
  6. }
  7. //asort — 对数组进行排序并保持索引关系
  8. $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
  9. asort($fruits);
  10. foreach ($fruits as $key => $val) {
  11. echo "$key = $val\n";
  12. }
  13. //krsort — 对数组按照键名逆向排序
  14. $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
  15. krsort($fruits);
  16. foreach ($fruits as $key => $val) {
  17. echo "$key = $val\n";
  18. }
  19. //ksort — 对数组按照键名排序
  20. $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
  21. ksort($fruits);
  22. foreach ($fruits as $key => $val) {
  23. echo "$key = $val\n";
  24. }
  25. //rsort — 对数组逆向排序
  26. $fruits = array("lemon", "orange", "banana", "apple");
  27. rsort($fruits);
  28. foreach ($fruits as $key => $val) {
  29. echo "$key = $val\n";
  30. }
  31. //sort — 对数组排序
  32. $fruits = array("lemon", "orange", "banana", "apple");
  33. sort($fruits);
  34. foreach ($fruits as $key => $val) {
  35. echo "$key = $val\n";
  36. }
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post