简体   繁体   English

如何排序版本信息

[英]How to sort versioning info

What's the right way, to handle versioning indicators like 2.4 or 2.4.0.9 etc. to get the ability of sorting versions. 什么是正确的方法,处理版本指标,如2.42.4.0.9等,以获得排序版本的能力。

PHP says, that 1.3.4 is not a valid integer, but also a non-valid number. PHP说, 1.3.4不是有效的整数,但也是无效的数字。

array('2.4','2.3.4','2.4.0.9')

PHP has a version_compare function. PHP有一个version_compare函数。 Use usort to sort it. 使用usort对其进行排序。 Like following. 喜欢以下。 :) :)

$a = array('2.4','2.3.4','2.4.0.9');
usort($a, 'version_compare');

Or, just use natsort : 或者,只需使用natsort

$array = array('2.4','2.16.6','2.3.4','2.4.0.9');
natsort($array);
print_r($array);

#Array ( [2] => 2.3.4 [0] => 2.4 [3] => 2.4.0.9 [1] => 2.16.6 )

Storing it as a string allows you to make use of the version_compare() function: 将其存储为字符串允许您使用version_compare()函数:

$versions = array('2.4','2.3.4','2.4.0.9');
usort($versions, 'version_compare');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM