/**
* 获取正确版本号
*/
if (!function_exists('versionFilter')) {
function versionFilter(?string $version): string
{
$version = $version ? trim($version) : '';
if (!$version) return '';
$pattern = '/\d+(?:[\.|\-|\_]\d+)+/';
$result = preg_match($pattern, $version, $matches);
if ($result) {
$version = $matches[0];
}
return $version;
}
}
/**
* 软件版本号排序
*/
function versionSort($versions, $column = 'version', $sort = 'DESC')
{
usort($versions, function ($a, $b) use ($column, $sort) {
$versionA = $a[$column];
$versionB = $b[$column];
if ($sort === 'DESC') {
return version_compare(versionFilter($versionB), versionFilter($versionA));
} else {
return version_compare(versionFilter($versionA), versionFilter($versionB));
}
});
return $versions;
}
$version = [
[
"id" => 259256,
"app_hub_id" => 11142,
"version" => "1.0.1"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "1.0.4"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "1.0.5"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "1.0.5.1"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "1.0.6"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "1.0.6.1"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "1.0.6.2"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "2.0"
],
[
"id" => 11637,
"app_hub_id" => 11142,
"version" => "12.0"
]
];
$info = $this->versionSort($version);
var_export($info);
array (
0 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '12.0',
),
1 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '2.0',
),
2 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '1.0.6.2',
),
3 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '1.0.6.1',
),
4 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '1.0.6',
),
5 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '1.0.5.1',
),
6 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '1.0.5',
),
7 =>
array (
'id' => 11637,
'app_hub_id' => 11142,
'version' => '1.0.4',
),
8 =>
array (
'id' => 259256,
'app_hub_id' => 11142,
'version' => '1.0.1',
),
)