Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 请解释以下排序功能_Php_Sorting - Fatal编程技术网

Php 请解释以下排序功能

Php 请解释以下排序功能,php,sorting,Php,Sorting,此函数按数组元素大小的递增顺序对数组进行排序,如果大小相等,则根据字典顺序进行排序 请有人帮忙,提前谢谢:) 大致如下: // declare the function function lensort($a,$b){ // Set $LA to the length of string $a $la = strlen( $a); // Set $Lb to the length of string $b $lb = strlen( $b); // If both strings are

此函数按数组元素大小的递增顺序对数组进行排序,如果大小相等,则根据字典顺序进行排序

请有人帮忙,提前谢谢:)

大致如下:

// declare the function
function lensort($a,$b){
// Set $LA to the length of string $a
$la = strlen( $a); 

// Set $Lb to the length of string $b
$lb = strlen( $b);

// If both strings are of equal length
if( $la == $lb) {
   // Return a neg or pos number based on which of the two strings is larger, alfabetically seen
    return strcmp( $a, $b);
}
// If not the same length, just return the size of one minus the other
return $la - $lb;
}

// Use usort to perform a person function-based sorting routine on the input data.
usort($array,'lensort');

如果
$a
小于
$b
,则传递给usort的函数将返回小于零的整数;如果
$a==$b
,则返回0;如果
$a
大于
$b
,则返回大于0

在这种情况下,它使用的是
$la-$lb
,因为这将根据
$a
$b
的长度差返回一个合适的整数。如果长度相同,则使用strcmp,该函数还将返回适当的整数。

该函数使用用户定义的比较函数按值对给定数组进行排序

用户定义的函数必须返回

  • 如果其参数相等,则为0
  • 如果第一个参数小于第二个参数,则整数值小于零
  • 如果第一个参数大于第二个参数,则整数值大于零

在代码段中,此用户定义的比较函数是
lensort
,它根据给定字符串的长度进行比较。

根据长度对字符串数组进行排序;如果字符串长度相等,则按字母顺序排序。。。。那么问题是什么?函数的哪一部分让您感到困惑?只有5行,没有一行能做任何复杂的事情。“我很感激你的回答,但我想知道,如果有人能写一个代码来完成同样的任务,而不是使用内置函数”-这不是一个合适的SO请求。我把你记下来了,因为一个合适的SO请求不是让人写你的代码,但在与编程相关的问题上提供协助和合作。我建议尝试一下,然后当你遇到困难时,在这里发帖,这样有人可以帮助你克服这个问题。
// declare the function
function lensort($a,$b){
// Set $LA to the length of string $a
$la = strlen( $a); 

// Set $Lb to the length of string $b
$lb = strlen( $b);

// If both strings are of equal length
if( $la == $lb) {
   // Return a neg or pos number based on which of the two strings is larger, alfabetically seen
    return strcmp( $a, $b);
}
// If not the same length, just return the size of one minus the other
return $la - $lb;
}

// Use usort to perform a person function-based sorting routine on the input data.
usort($array,'lensort');