如何版本比较php中的每个数组值?

发布于 2024-11-08 12:54:47 字数 736 浏览 2 评论 0原文

我有两个数组需要与运算符 <= 进行比较。我认为尝试执行此操作的一个简单方法是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-11-15 12:54:47
    foreach( array_keys( $a ) AS $key ) {            
        if( version_compare($a[$key], $b[$key], '<=')) { print "we have a problem with: " . $a[$key] . "\n"; }
    }
    foreach( array_keys( $a ) AS $key ) {            
        if( version_compare($a[$key], $b[$key], '<=')) { print "we have a problem with: " . $a[$key] . "\n"; }
    }
逆蝶 2024-11-15 12:54:47

我为您制作了一个简单的课程,以尽可能简单地解决您的问题。

类文件(class.myversion.php)

<?php
class MyVersion
{
    private $_version;
    private $_name;

    public function __construct($_name, $_version)
    {
        $this->_version = $_version;
        $this->_name = $_name;
    }
    public function getVersion()
    {
        return $this->_version;
    }
    public function getName()
    {
        return $this->_name;
    }
}
?>

和测试文件(test.php)

require 'class.myversion.php';

$a = Array();
$b = Array();

$a[] = new MyVersion("ajax dropdown", 15); // 15 means 0.1.5
$a[] = new MyVersion("hello dolly", 16);
$a[] = new MyVersion("test", 45);

$b[] = new MyVersion("ajax dropdown", 14); // 14 means 0.1.4
$b[] = new MyVersion("hello dolly", 16);
$b[] = new MyVersion("test", 46);

for($i = 0; $i < sizeof($a); $i++)
    if($a[$i]->getVersion() < $b[$i]->getVersion())
        echo "(".$a[$i]->getName().")needs to get Updated. Required version: ".$b[$i]->getVersion()."<br />";
    elseif($a[$i]->getVersion() > $b[$i]->getVersion())
        echo "(".$b[$i]->getName().")needs to get Updated. Required version: ".$a[$i]->getVersion()."<br />";
?>

就是这样!如果你认为它很难理解,我可以解释它,但我认为它很容易理解。

I have made a simple Class for you to solve your problem as easy as its posibble.

the Class file (class.myversion.php)

<?php
class MyVersion
{
    private $_version;
    private $_name;

    public function __construct($_name, $_version)
    {
        $this->_version = $_version;
        $this->_name = $_name;
    }
    public function getVersion()
    {
        return $this->_version;
    }
    public function getName()
    {
        return $this->_name;
    }
}
?>

And the Test File (test.php)

require 'class.myversion.php';

$a = Array();
$b = Array();

$a[] = new MyVersion("ajax dropdown", 15); // 15 means 0.1.5
$a[] = new MyVersion("hello dolly", 16);
$a[] = new MyVersion("test", 45);

$b[] = new MyVersion("ajax dropdown", 14); // 14 means 0.1.4
$b[] = new MyVersion("hello dolly", 16);
$b[] = new MyVersion("test", 46);

for($i = 0; $i < sizeof($a); $i++)
    if($a[$i]->getVersion() < $b[$i]->getVersion())
        echo "(".$a[$i]->getName().")needs to get Updated. Required version: ".$b[$i]->getVersion()."<br />";
    elseif($a[$i]->getVersion() > $b[$i]->getVersion())
        echo "(".$b[$i]->getName().")needs to get Updated. Required version: ".$a[$i]->getVersion()."<br />";
?>

Thats it! If you think its hard to understand, i can explain it but i think its easy enought to understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文