<?php
$names = array(
"Apple 12 Pro Max",
"Samsung Galaxy S21",
"Apple 11 Pro",
"OnePlus 9",
"Google Pixel 5",
"Xiaomi Mi 11",
);
// 自定义排序函数
function cmp($a, $b) {
// 从字符串中提取数字
preg_match('/\d+/', $a, $a_matches);
preg_match('/\d+/', $b, $b_matches);
$a_number = $a_matches[0];
$b_number = $b_matches[0];
// 比较数字
if ($a_number == $b_number) {
return strcmp($a, $b); // 数字相同,按名称排序
} else {
return $a_number - $b_number; // 按数字排序
}
}
// 使用自定义排序函数进行排序
usort($names, "cmp");
// 输出排序结果
foreach ($names as $name) {
echo $name . "<br>";
}
?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
请登录后发表评论
注册
社交帐号登录