array_multisort() 是一个内置的 PHP 函数,它可以对多个数组或多维数组进行排序
<?php
// 示例数组
$data = [
['volume' => 67, 'edition' => 2],
['volume' => 86, 'edition' => 1],
['volume' => 85, 'edition' => 6],
['volume' => 98, 'edition' => 2],
['volume' => 86, 'edition' => 6]
];
// 提取需要排序的列
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// 对 volume 和 edition 列进行排序
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
// 打印排序后的数组
print_r($data);
?>
在这个示例中,我们首先创建了一个包含多个关联子数组的 $data 数组。然后,我们使用 foreach 循环遍历 $data 数组,并将 volume 和 edition 列的值分别存储到 $volume 和 $edition 数组中。
接下来,我们使用 array_multisort() 函数对 $volume 和 $edition 数组进行排序。我们传递了五个参数:
$volume - 第一个排序的数组。SORT_DESC - 指定按降序对 $volume 数组进行排序。$edition - 第二个排序的数组。SORT_ASC - 指定按升序对 $edition 数组进行排序。$data - 要根据前面指定的排序规则对其进行排序的原始数组。最后,我们使用 print_r() 函数打印排序后的 $data 数组。