蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 PHP 8.4 六大数组排序函数、稳定排序与排序标志实验

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-arrays-sort.php
explore-arrays-sort.php117 行
<?php
declare(strict_types=1);

$cases = [];

// Case 1: sort() ascending for strings and numbers + key re-indexing
$cars = ['Volvo', 'BMW', 'Toyota'];
$okCars = sort($cars);
$numbers = [4, 6, 2, 22, 11];
$okNums = sort($numbers);
$assocReindex = ['b' => 20, 'a' => 10];
sort($assocReindex);

$c1Actual = [
    'cars' => $cars,
    'numbers' => $numbers,
    'returns_true' => ($okCars && $okNums),
    'reindexed_keys' => array_keys($assocReindex),
];
$c1Expected = [
    'cars' => ['BMW', 'Toyota', 'Volvo'],
    'numbers' => [2, 4, 6, 11, 22],
    'returns_true' => true,
    'reindexed_keys' => [0, 1],
];
$cases[] = [
    'name' => 'sort_ascending_alpha_and_num',
    'passed' => $c1Actual === $c1Expected,
    'expected' => $c1Expected,
    'actual' => $c1Actual,
];

// Case 2: rsort() descending for strings and numbers
$carsDesc = ['Volvo', 'BMW', 'Toyota'];
rsort($carsDesc);
$numsDesc = [4, 6, 2, 22, 11];
rsort($numsDesc);

$c2Actual = [
    'cars' => $carsDesc,
    'numbers' => $numsDesc,
];
$c2Expected = [
    'cars' => ['Volvo', 'Toyota', 'BMW'],
    'numbers' => [22, 11, 6, 4, 2],
];
$cases[] = [
    'name' => 'rsort_descending_alpha_and_num',
    'passed' => $c2Actual === $c2Expected,
    'expected' => $c2Expected,
    'actual' => $c2Actual,
];

// Case 3: asort() by value ascending & ksort() by key ascending (preserving associations)
$ageByVal = ['Peter' => '35', 'Ben' => '37', 'Joe' => '43'];
asort($ageByVal);
$ageUnsorted = ['Joe' => '43', 'Peter' => '35', 'Ben' => '37'];
asort($ageUnsorted);
$ageByKey = ['Peter' => '35', 'Ben' => '37', 'Joe' => '43'];
ksort($ageByKey);

$c3Actual = [
    'asort_keys' => array_keys($ageByVal),
    'asort_from_unsorted_keys' => array_keys($ageUnsorted),
    'asort_values' => array_values($ageUnsorted),
    'ksort_keys' => array_keys($ageByKey),
    'ksort_values' => array_values($ageByKey),
];
$c3Expected = [
    'asort_keys' => ['Peter', 'Ben', 'Joe'],
    'asort_from_unsorted_keys' => ['Peter', 'Ben', 'Joe'],
    'asort_values' => ['35', '37', '43'],
    'ksort_keys' => ['Ben', 'Joe', 'Peter'],
    'ksort_values' => ['37', '43', '35'],
];
$cases[] = [
    'name' => 'asort_and_ksort_ascending',
    'passed' => $c3Actual === $c3Expected,
    'expected' => $c3Expected,
    'actual' => $c3Actual,
];

// Case 4: arsort() & krsort() descending + PHP 8 stable sort & SORT_STRING vs SORT_NUMERIC
$ageValDesc = ['Peter' => '35', 'Ben' => '37', 'Joe' => '43'];
arsort($ageValDesc);
$ageKeyDesc = ['Peter' => '35', 'Ben' => '37', 'Joe' => '43'];
krsort($ageKeyDesc);
$stable = ['first' => 10, 'second' => 5, 'third' => 10];
asort($stable);
$strFlag = ['10', '2', '1'];
sort($strFlag, SORT_STRING);
$numFlag = ['10', '2', '1'];
sort($numFlag, SORT_NUMERIC);

$c4Actual = [
    'arsort_keys' => array_keys($ageValDesc),
    'krsort_keys' => array_keys($ageKeyDesc),
    'stable_keys' => array_keys($stable),
    'sort_string' => $strFlag,
    'sort_numeric' => $numFlag,
];
$c4Expected = [
    'arsort_keys' => ['Joe', 'Ben', 'Peter'],
    'krsort_keys' => ['Peter', 'Joe', 'Ben'],
    'stable_keys' => ['second', 'first', 'third'],
    'sort_string' => ['1', '10', '2'],
    'sort_numeric' => ['1', '2', '10'],
];
$cases[] = [
    'name' => 'arsort_and_krsort_descending',
    'passed' => $c4Actual === $c4Expected,
    'expected' => $c4Expected,
    'actual' => $c4Actual,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-arrays-sort-lab/explore-arrays-sort.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-arrays-sort.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1231.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习