简体   繁体   English

将javascript函数转换为php(自定义排序)

[英]Convert javascript function to php (custom sort)

I have this code in php. 我在php中有此代码。

$nameList = array();
for ($j = 1; $j <= nameList; $j++) {
    //DO the sort here
}
return $nameList;

Now I want to do the below javascript but I want to do it on the PHP array $nameList, so how can I do the below code in PHP syntax? 现在,我想执行以下javascript,但要在PHP数组$ nameList上执行,那么如何使用PHP语法执行以下代码? That is I want to create a regex where I filter away all digits on each row and compare the letters where the "higher" letters come first in the array. 那就是我想创建一个正则表达式,在其中过滤掉每一行的所有数字并比较“较高”字母在数组中排在首位的字母。

var nRegex = /\d/g;
nameList.sort(function(a, b) { a = a.replace(nRegex, ""); b = b.replace(nRegex, ""); 
return b.localeCompare(a); 
});
$nRegex = '/\d/';

function cmp($a, $b) {

    $a = preg_replace($nRegex, '', $a );
    $b = preg_replace($nRegex, '', $b );

    return strcmp($a, $b);
}

usort($a, "cmp");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM