如何版本比较php中的每个数组值?
我有两个数组需要与运算符 <=
进行比较。我认为尝试执行此操作的一个简单方法是使用 version_compare
但我不确定 A. 这是最好的方法和 B. 它实际上是在比较正确的值。
为了让 version_compare 工作,我将数组内爆。
//Original arrays.
a$ = array( 0 => "ajax dropdown0.1.5", 1 => "hello dolly1.6", 2 => "test4.5");
b$ = array( 0 => "ajax dropdown0.1.4", 1 => "hello dolly1.6", 2 => "test4.6");
//implode into string
$a_implode = implode( "," , $a );
$b_implode = implode( "," , $b );
//compare version
if (version_compare($a_implode, $b_implode, '<=')){
echo 'We have a problem';
}
这似乎有效,但我不知道它是否实际上比较了正确的值,例如 test4.5 必须仅与 test4.6 进行比较(而不是其他字符串值),而且我不确定如何输出任何匹配项,如果 version_compare返回真。
I have two arrays I need to compare with the operator <=
. I thought an easy way to try and do this is using version_compare
but I'm not sure that A. This is best method and B. It's actually comparing the right values.
In order for version_compare to work I implode the array.
//Original arrays.
a$ = array( 0 => "ajax dropdown0.1.5", 1 => "hello dolly1.6", 2 => "test4.5");
b$ = array( 0 => "ajax dropdown0.1.4", 1 => "hello dolly1.6", 2 => "test4.6");
//implode into string
$a_implode = implode( "," , $a );
$b_implode = implode( "," , $b );
//compare version
if (version_compare($a_implode, $b_implode, '<=')){
echo 'We have a problem';
}
This seems to work but I have no idea if it's actually comparing the correct values, for instance test4.5 must only be compared to test4.6 ( and not the other string values), also I am unsure how to output any matches if version_compare returns true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我为您制作了一个简单的课程,以尽可能简单地解决您的问题。
类文件(class.myversion.php)
和测试文件(test.php)
就是这样!如果你认为它很难理解,我可以解释它,但我认为它很容易理解。
I have made a simple Class for you to solve your problem as easy as its posibble.
the Class file (class.myversion.php)
And the Test File (test.php)
Thats it! If you think its hard to understand, i can explain it but i think its easy enought to understand.