Blogger Information
Blog 17
fans 1
comment 0
visits 14433
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组的排序, 数组的合并, 数组成员的统计
zl的php学习博客
Original
719 people have browsed it

1. 数组的排序:

排序函数的参数相同(数组, 比较的类型,默认正常)

1.正向排序:sort()

  1. $arrSort = [];
  2. for ($i = 0; $i < 100; $i++) {
  3. $arrSort[] = (int)(lcg_value() * 1000);
  4. }
  5. //echo '排序前:', print_r($arrSort), '<br>';
  6. echo '排序前:<pre>' . print_r($arrSort, true) . '</pre><br>';
  7. sort($arrSort);
  8. //echo '排序后:', print_r($arrSort), '<br>';
  9. echo '排序后:<pre>' . print_r($arrSort, true) . '</pre><br>';

2.逆向排序:rsort()

  1. $arrSort = [];
  2. for ($i = 0; $i < 100; $i++) {
  3. $arrSort[] = (int)(lcg_value() * 1000);
  4. }
  5. //echo '排序前:', print_r($arrSort), '<br>';
  6. echo '排序前:<pre>' . print_r($arrSort, true) . '</pre><br>';
  7. rsort($arrSort);
  8. //echo '排序后:', print_r($arrSort), '<br>';
  9. echo '排序后:<pre>' . print_r($arrSort, true) . '</pre><br>';
  1. ##3.按照键名正向排序:ksort()
  2. ```php
  3. $arr105 = ['101' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10];
  4. echo '排序前:<pre>' . print_r($arr105, true) . '</pre><br>';
  5. ksort($arr105);
  6. echo '排序后:<pre>' . print_r($arr105, true) . '</pre><br>';

4.按照键名逆向排序:krsort()

  1. $arr105 = ['101' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10];
  2. echo '排序前:<pre>' . print_r($arr105, true) . '</pre><br>';
  3. krsort($arr105);
  4. echo '排序后:<pre>' . print_r($arr105, true) . '</pre><br>';

5.正向排序并保持索引关系(按照值排序):asort()

  1. $arrAsort = ['101' => 101, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 10, '9' => 9, '10' => 8];
  2. echo '排序前:<pre>' . print_r($arrAsort, true) . '</pre><br>';
  3. asort($arrAsort);
  4. echo '排序后:<pre>' . print_r($arrAsort, true) . '</pre><br>';

6.逆向排序并保持索引关系(按照值排序):arsort()

  1. $arrArsort = ['101' => 101, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 10, '9' => 9, '10' => 8];
  2. echo '排序前:<pre>' . print_r($arrArsort, true) . '</pre><br>';
  3. arsort($arrArsort);
  4. echo '排序后:<pre>' . print_r($arrArsort, true) . '</pre><br>';

7.多个数组或多维数组进行排序:array_multisort()

  1. $arr9 = [
  2. ['id' => 3, 'name' => 'user', 'age' => 22],
  3. ['id' => 1, 'name' => 'admin', 'age' => 18],
  4. ['id' => 5, 'name' => 'user', 'age' => 26],
  5. ['id' => 2, 'name' => 'user', 'age' => 20],
  6. ['id' => 4, 'name' => 'user', 'age' => 24],
  7. ['id' => 6, 'name' => 'user', 'age' => 28],
  8. ];
  9. echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
  10. array_multisort($arr9);
  11. echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';

8.自定义排序

  1. // usort — 使用用户自定义的比较函数对数组中的值进行排序(sort函数的进阶版)
  2. // uksort — 使用用户自定义的比较函数对数组中的键名进行排序(ksort函数的进阶版)
  3. // uasort — 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联(asort函数的进阶版)
  4. $arrusort = [10,2,11,22,3,4];
  5. echo '排序前:<pre>' . print_r($arrusort, true) . '</pre><br>';
  6. usort($arrusort, function ($a, $b) {
  7. if ($a === $b) return 0;
  8. return $a < $b ? 1 : -1;
  9. });
  10. echo '排序后:<pre>' . print_r($arrusort, true) . '</pre><br>';
  11. $arr9 = [
  12. ['id' => 3, 'name' => 'user', 'age' => 22],
  13. ['id' => 1, 'name' => 'admin', 'age' => 18],
  14. ['id' => 5, 'name' => 'user', 'age' => 26],
  15. ['id' => 2, 'name' => 'user', 'age' => 20],
  16. ['id' => 4, 'name' => 'user', 'age' => 24],
  17. ['id' => 6, 'name' => 'user', 'age' => 28],
  18. ];
  19. //echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
  20. //usort($arr9, function ($a, $b) {
  21. // if ($a['id'] === $b['id']) return 0;
  22. // return $a['id'] < $b['id'] ? 1 : -1;
  23. //});
  24. //echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';
  25. //
  26. //echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
  27. //uasort($arr9, function ($a, $b) {
  28. // if ($a['id'] === $b['id']) return 0;
  29. // return $a['id'] < $b['id'] ? 1 : -1;
  30. //});
  31. //echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';
  32. //
  33. echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
  34. uasort($arr9, function ($a, $b) {
  35. if ($a['id'] === $b['id']) return 0;
  36. return $a['id'] < $b['id'] ? 1 : -1;
  37. });
  38. echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';

2. 数组的合并: array_merge()

  1. $arr105 = ['101' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10];
  2. $arr106 = ['10' => 10, '20' => 20, '30' => 30, '40' => 40, '50' => 50, '60' => 60, '70' => 70, '80' => 80, '90' => 90, '100' => 100];
  3. $arr107 = ['100' => 100, '200' => 200, '300' => 300, '400' => 400, '500' => 500, '600' => 600, '700' => 700, '800' => 800, '900' => 900, '1000' => 1000];
  4. $arr108 = ['1000' => 1000, '2000' => 2000, '3000' => 3000, '4000' => 4000, '5000' => 5000, '6000' => 6000, '7000' => 7000, '800' => 8000, '9000' => 9000, '10000' => 10000];
  5. $result = array_merge($arr105,$arr106,$arr107,$arr108);
  6. echo print_r($result), '<br>';
  7. echo '<pre>' . print_r($result, true) . '</pre><br>';

3. 数组成员的统计: array_count_values()

  1. // 数组成员的统计: array_count_values()
  2. // 补: array_column(数组, 返回的key和值): 获取数组中的指定key和它的值
  3. $arrCount = [];
  4. for ($i = 0; $i < 100; $i++) {
  5. $arrCount[] = (int)(lcg_value() * 10);
  6. }
  7. echo print_r($arrCount), '<br>';
  8. echo print_r(array_count_values($arrCount)), '<hr>';
  9. $arrCount1 = [
  10. ['id' => 3, 'name' => 'user', 'age' => 28],
  11. ['id' => 1, 'name' => 'admin', 'age' => 18],
  12. ['id' => 5, 'name' => 'user', 'age' => 26],
  13. ['id' => 2, 'name' => 'user', 'age' => 18],
  14. ['id' => 4, 'name' => 'user', 'age' => 26],
  15. ['id' => 6, 'name' => 'user', 'age' => 28],
  16. ];
  17. echo print_r($arrCount1), '<br>';
  18. echo print_r(array_count_values(array_column($arrCount1, 'age'))), '<hr>';
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