PHP 中的 strcmp 相当于整数 (intcmp)

发布于 2024-09-01 17:06:42 字数 924 浏览 4 评论 0原文

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

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

,但是我们没有 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;
}

这感觉很脏。大家觉得怎么样?

这是一个类的一部分,用于通过传入的排序值对 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;
    }
    ....
}

So we got this function in PHP

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

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

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.

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;
    }
    ....
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

久伴你 2024-09-08 17:06:42

对数据进行排序:

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

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

如果相关数字超出 PHP 的整数范围,则返回 ($a < $b) ? -1 : (($a > $b) ? 1 : 0) 更稳健。

Sort your data with:

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

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

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

ま柒月 2024-09-08 17:06:42

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

因此,比较函数将类似于...

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

这里的一些非常好的功能是,比较的完成方式与所有其他比较完全相同。所以类型杂耍将会如期发生。

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

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

Purely as some additional information, there has been an accepted RFC for this (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.

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

蓝梦月影 2024-09-08 17:06:42

为什么要重新发明轮子?
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

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

why reinventing the wheel?
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

靑春怀旧 2024-09-08 17:06:42

你可以使用

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

虽然我根本不明白使用这个功能的意义

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

东北女汉子 2024-09-08 17:06:42

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

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

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.

return (int) $a > (int) $b ? 1 : (int) $a == (int) $b ? 0 : -1;
如日中天 2024-09-08 17:06:42

乍一看,是的,感觉很脏。除非您这样写必须有一个充分的理由,而不是仅仅使用实际的 ==><< /代码> 运算符。创建这个功能的动机是什么?

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

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

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

At a glance, yes it feels dirty. Except there must be a good reason 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!

忆悲凉 2024-09-08 17:06:42

对于字符串

 usort($points, function ($a, $b) use ($orderFlag, $key1, $key2) {
        return strcmp($a[$key1][$key2], $b[$key1][$key2]) * $orderFlag;
    });

orderFlag => 1(升序):-1(降序)

对于数字

usort($points, function ($a, $b) use ($orderFlag, $key1, $key2) {
    return ($a[$key1][$key2] - $b[$key1][$key2]) * $orderFlag;
});

orderFlag => 1(升序):-1(降序)

For strings

 usort($points, function ($a, $b) use ($orderFlag, $key1, $key2) {
        return strcmp($a[$key1][$key2], $b[$key1][$key2]) * $orderFlag;
    });

orderFlag => 1 (ascending): -1 (descending)

For numbers

usort($points, function ($a, $b) use ($orderFlag, $key1, $key2) {
    return ($a[$key1][$key2] - $b[$key1][$key2]) * $orderFlag;
});

orderFlag => 1 (ascending): -1 (descending)

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