Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
排序函数的参数相同(数组, 比较的类型,默认正常)
$arrSort = [];
for ($i = 0; $i < 100; $i++) {
$arrSort[] = (int)(lcg_value() * 1000);
}
//echo '排序前:', print_r($arrSort), '<br>';
echo '排序前:<pre>' . print_r($arrSort, true) . '</pre><br>';
sort($arrSort);
//echo '排序后:', print_r($arrSort), '<br>';
echo '排序后:<pre>' . print_r($arrSort, true) . '</pre><br>';
$arrSort = [];
for ($i = 0; $i < 100; $i++) {
$arrSort[] = (int)(lcg_value() * 1000);
}
//echo '排序前:', print_r($arrSort), '<br>';
echo '排序前:<pre>' . print_r($arrSort, true) . '</pre><br>';
rsort($arrSort);
//echo '排序后:', print_r($arrSort), '<br>';
echo '排序后:<pre>' . print_r($arrSort, true) . '</pre><br>';
##3.按照键名正向排序:ksort()
```php
$arr105 = ['101' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10];
echo '排序前:<pre>' . print_r($arr105, true) . '</pre><br>';
ksort($arr105);
echo '排序后:<pre>' . print_r($arr105, true) . '</pre><br>';
$arr105 = ['101' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10];
echo '排序前:<pre>' . print_r($arr105, true) . '</pre><br>';
krsort($arr105);
echo '排序后:<pre>' . print_r($arr105, true) . '</pre><br>';
$arrAsort = ['101' => 101, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 10, '9' => 9, '10' => 8];
echo '排序前:<pre>' . print_r($arrAsort, true) . '</pre><br>';
asort($arrAsort);
echo '排序后:<pre>' . print_r($arrAsort, true) . '</pre><br>';
$arrArsort = ['101' => 101, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 10, '9' => 9, '10' => 8];
echo '排序前:<pre>' . print_r($arrArsort, true) . '</pre><br>';
arsort($arrArsort);
echo '排序后:<pre>' . print_r($arrArsort, true) . '</pre><br>';
$arr9 = [
['id' => 3, 'name' => 'user', 'age' => 22],
['id' => 1, 'name' => 'admin', 'age' => 18],
['id' => 5, 'name' => 'user', 'age' => 26],
['id' => 2, 'name' => 'user', 'age' => 20],
['id' => 4, 'name' => 'user', 'age' => 24],
['id' => 6, 'name' => 'user', 'age' => 28],
];
echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
array_multisort($arr9);
echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';
// usort — 使用用户自定义的比较函数对数组中的值进行排序(sort函数的进阶版)
// uksort — 使用用户自定义的比较函数对数组中的键名进行排序(ksort函数的进阶版)
// uasort — 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联(asort函数的进阶版)
$arrusort = [10,2,11,22,3,4];
echo '排序前:<pre>' . print_r($arrusort, true) . '</pre><br>';
usort($arrusort, function ($a, $b) {
if ($a === $b) return 0;
return $a < $b ? 1 : -1;
});
echo '排序后:<pre>' . print_r($arrusort, true) . '</pre><br>';
$arr9 = [
['id' => 3, 'name' => 'user', 'age' => 22],
['id' => 1, 'name' => 'admin', 'age' => 18],
['id' => 5, 'name' => 'user', 'age' => 26],
['id' => 2, 'name' => 'user', 'age' => 20],
['id' => 4, 'name' => 'user', 'age' => 24],
['id' => 6, 'name' => 'user', 'age' => 28],
];
//echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
//usort($arr9, function ($a, $b) {
// if ($a['id'] === $b['id']) return 0;
// return $a['id'] < $b['id'] ? 1 : -1;
//});
//echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';
//
//echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
//uasort($arr9, function ($a, $b) {
// if ($a['id'] === $b['id']) return 0;
// return $a['id'] < $b['id'] ? 1 : -1;
//});
//echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';
//
echo '排序前:<pre>' . print_r($arr9, true) . '</pre><br>';
uasort($arr9, function ($a, $b) {
if ($a['id'] === $b['id']) return 0;
return $a['id'] < $b['id'] ? 1 : -1;
});
echo '排序后:<pre>' . print_r($arr9, true) . '</pre><br>';
$arr105 = ['101' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10];
$arr106 = ['10' => 10, '20' => 20, '30' => 30, '40' => 40, '50' => 50, '60' => 60, '70' => 70, '80' => 80, '90' => 90, '100' => 100];
$arr107 = ['100' => 100, '200' => 200, '300' => 300, '400' => 400, '500' => 500, '600' => 600, '700' => 700, '800' => 800, '900' => 900, '1000' => 1000];
$arr108 = ['1000' => 1000, '2000' => 2000, '3000' => 3000, '4000' => 4000, '5000' => 5000, '6000' => 6000, '7000' => 7000, '800' => 8000, '9000' => 9000, '10000' => 10000];
$result = array_merge($arr105,$arr106,$arr107,$arr108);
echo print_r($result), '<br>';
echo '<pre>' . print_r($result, true) . '</pre><br>';
// 数组成员的统计: array_count_values()
// 补: array_column(数组, 返回的key和值): 获取数组中的指定key和它的值
$arrCount = [];
for ($i = 0; $i < 100; $i++) {
$arrCount[] = (int)(lcg_value() * 10);
}
echo print_r($arrCount), '<br>';
echo print_r(array_count_values($arrCount)), '<hr>';
$arrCount1 = [
['id' => 3, 'name' => 'user', 'age' => 28],
['id' => 1, 'name' => 'admin', 'age' => 18],
['id' => 5, 'name' => 'user', 'age' => 26],
['id' => 2, 'name' => 'user', 'age' => 18],
['id' => 4, 'name' => 'user', 'age' => 26],
['id' => 6, 'name' => 'user', 'age' => 28],
];
echo print_r($arrCount1), '<br>';
echo print_r(array_count_values(array_column($arrCount1, 'age'))), '<hr>';