strcmp equivelant for integers (intcmp) in PHP(PHP 中整数 (intcmp) 的 strcmp 等效项)
问题描述
所以我们在 PHP 中得到了这个函数
So we got this function in PHP
strcmp(string $1,string $2) // returns -1,0, or 1;
然而,我们没有 intcmp();所以我创建了一个:
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?
这是通过传入的排序值对 Javascript 进行排序的类的一部分.
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;
}
....
}
推荐答案
对数据进行排序:
function sortScripts($a, $b)
{
return $a['order'] - $b['order'];
}
如果您想要颠倒的顺序,请使用 $b-$a.
Use $b-$a if you want the reversed order.
如果有问题的数字超出 PHP 的整数范围,return ($a < $b) ?-1 : (($a > $b) ? 1 : 0) 更健壮.
If the numbers in question exceed PHP's integer range, return ($a < $b) ? -1 : (($a > $b) ? 1 : 0) is more robust.
这篇关于PHP 中整数 (intcmp) 的 strcmp 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 中整数 (intcmp) 的 strcmp 等效项
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
