PHP 中整数 (intcmp) 的 strcmp 等效项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2852621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:54:39  来源:igfitidea点击:

strcmp equivelant for integers (intcmp) in PHP

phpfunctionintegercompare

提问by Chase Wilson

So we got this function in PHP

所以我们在 PHP 中得到了这个函数

strcmp(string ,string ) // returns -1,0, or 1;

We Do not however, have an intcmp(); So i created one:

但是,我们没有 intcmp(); 所以我创建了一个:

function intcmp($a,$b) {
    if((int)$a == (int)$b)return 0;
    if((int)$a  > (int)$b)return 1;
    if((int)$a  < (int)$b)return -1;
}

This just feels dirty. What do you all think?

这只是感觉很脏。大家怎么看?

this is part of a class to sort Javascripts by an ordering value passed in.

这是通过传入的排序值对 Javascript 进行排序的类的一部分。

class JS
{
    // array('order'=>0,'path'=>'/js/somefile.js','attr'=>array());
    public $javascripts = array(); 
    ...
    public function __toString()
    {
        uasort($this->javascripts,array($this,'sortScripts'));
        return $this->render();
    }
    private function sortScripts($a,$b)
    {
        if((int)$a['order'] == (int)$b['order']) return 0;
        if((int)$a['order'] > (int)$b['order']) return 1;
        if((int)$a['order'] < (int)$b['order']) return -1;
    }
    ....
}

回答by Nicolas Viennot

Sort your data with:

使用以下方法对数据进行排序:

function sortScripts($a, $b)
{
    return $a['order'] - $b['order'];
}

Use $b-$a if you want the reversed order.

如果您想要相反的顺序,请使用 $b-$a。

If the numbers in question exceed PHP's integer range, return ($a < $b) ? -1 : (($a > $b) ? 1 : 0)is more robust.

如果有问题的数字超出 PHP 的整数范围,return ($a < $b) ? -1 : (($a > $b) ? 1 : 0)则更健壮。

回答by nico

You could use

你可以用

function intcmp($a,$b)
    {
    return ($a-$b) ? ($a-$b)/abs($a-$b) : 0;
    }

Although I don't see the point in using this function at all

虽然我根本看不出使用这个功能的意义

回答by Gabriele Manganello

why reinventing the wheel? http://php.net/manual/en/function.strnatcmp.php

为什么要重新发明轮子? http://php.net/manual/en/function.strnatcmp.php

echo strnatcmp(1, 2) . PHP_EOL; // -1
echo strnatcmp(10, 2) . PHP_EOL; // 1
echo strnatcmp(10.5, 2) . PHP_EOL; // 1 - work with float numbers
echo strnatcmp(1, -2) . PHP_EOL; // 1 - work with negative numbers

Test it here: https://3v4l.org/pSANR

在这里测试:https: //3v4l.org/pSANR

回答by Richard A Quadling

Purely as some additional information, there has been an accepted RFC for this (https://wiki.php.net/rfc/combined-comparison-operator).

纯粹作为一些附加信息,已经为此接受了 RFC ( https://wiki.php.net/rfc/combined-comparison-operator)。

So, the comparison function would be along the lines of ...

所以,比较函数将沿着......

<?php
$data = [...];
usort($data, function($left, $right){ return $left <=> $right; });
?>

A few really nice feature here is that the comparison is done in exactly the same way as all other comparisons. So type juggling will happen as expected.

这里有一些非常好的特性,比较的方式与所有其他比较完全相同。所以类型杂耍会按预期发生。

As yet, there is no magic __forCompare() like method to allow an object to expose a comparison value. The current proposal (a different RFC) is to have each object be injected into every other object during the comparison so that it does the comparison - something which just seems odd to me - potential opportunity for recursion and stack overflow ... ! I would have thought either injecting the type of object for comparison (allowing an object the ability to represent appropriate values depending upon the type of comparison) or a blind request for a value that the object can serve up for comparison, would have been a safer solution.

到目前为止,还没有像 __forCompare() 这样的神奇方法来允许对象公开比较值。当前的提议(不同的 RFC)是在比较期间将每个对象注入到每个其他对象中,以便它进行比较 - 这对我来说似乎很奇怪 - 递归和堆栈溢出的潜在机会......!我会认为要么注入对象的类型进行比较(允许对象能够根据比较类型表示适当的值)或盲目请求对象可以提供用于比较的值,会更安全解决方案。

Not yet integrated into PHP-NG (PHP 7 at the moment), but hopefully will be soon.

尚未集成到 PHP-NG(目前为 PHP 7)中,但希望很快就会集成。

回答by tomlogic

Does it have to be +1 and -1? If not, just return (int) $a - (int) $b. I don't like the divide that someone else recommended, and there's no need to check for all three cases. If it's not greater and not equal, it must be less than.

它必须是+1和-1吗?如果没有,只需返回(int) $a - (int) $b。我不喜欢其他人推荐的划分,也没有必要检查所有三种情况。如果它不大于也不等于,则必须小于。

return (int) $a > (int) $b ? 1 : (int) $a == (int) $b ? 0 : -1;

回答by JYelton

I wouldn't call it dirtyper se, it seems valid enough. But I can't think where I would use that function. My only suggestion might be to include else:

我不会把它本身称为肮脏的,它似乎足够有效。但我想不出我会在哪里使用这个功能。我唯一的建议可能是包括else

function intcmp($a,$b)
{
    if((int)$a == (int)$b)return 0;
    else if((int)$a  > (int)$b)return 1;
    else if((int)$a  < (int)$b)return -1;
}

回答by FrustratedWithFormsDesigner

At a glance, yes it feels dirty. Except there must be a goodreason you wrote that instead of just using the actual ==, >, and <operators. What was the motivation for creating this function?

乍一看,是的,感觉很脏。除了必须有一个很好的理由,你写的,而不是仅仅使用实际的 ==><运营商。创建这个函数的动机是什么?

If it were me, I'd probably just do something like:

如果是我,我可能会做这样的事情:

$x = $a==$b ? 0 : ($a>$b ? 1 : ($a<$b ? -1 : null));

I realize this is just as ugly, and the : null;- not sure if PHP requires it or if I could have just done :;but I don't like it and that code should never execute anyway... I think I'd be a lot less confused about this if I knew the original requirements!

我意识到这同样丑陋,而且: null;- 不确定 PHP 是否需要它,或者我是否可以完成,:;但我不喜欢它,而且代码无论如何都不应该执行......我想我会少很多如果我知道原始要求,对此感到困惑!