在PHP中,下面是一个数组示例:
$version = [
"x3x3x2",
"x10x11x0",
"x180x7x1",
"x10x1x7",
"x2x0x4",
"x3x3x1",
]
每个条目都遵循此模式'xNxNxN'(一个字符'x'和一个数字N,三次)
我想按数字从左到右对数组的每个条目进行排序。预期的结果是:
$version = [
"x2x0x4",
"x3x3x1",
"x3x3x2",
"x10x1x0",
"x10x11x7",
"x180x7x1",
]
我该怎么做?
答案 0 :(得分:2)
有一个built-in function to compare version numbers。只需将其与usort
组合即可。
usort($version, 'version_compare');
结果:
var_dump($version);
array(6) {
[0]=>
string(6) "x2x0x4"
[1]=>
string(6) "x3x3x1"
[2]=>
string(6) "x3x3x2"
[3]=>
string(7) "x10x1x7"
[4]=>
string(8) "x10x11x0"
[5]=>
string(8) "x180x7x1"
}
答案 1 :(得分:0)