php数组排序讲解之按照某个字段排序(实例)
2017-12-05 10:04:57         来源：一株马蹄莲的博客

Listing A

? \$data = array(5,8,1,7,2);

? sort(\$data);

? print_r(\$data);

? ?>

Array ([0] => 1

[1] => 2

[2] => 5

[3] => 7

[4] => 8

)

Listing B

?>

Array ([0] => 8

[1] => 7

[2] => 5

[3] => 2

[4] => 1

)

Listing C

"United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");ksort(\$data); print_r(\$data);

?>

Array ([DE] => Germany

[ES] => Spain

[IN] => India

[US] => United States

)

Krsort()函数是根据关键字对数组进行倒排，Listing D就是这样的例子：

Listing D

"United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");krsort(\$data); print_r(\$data);

?>

Array ([US] => United States

[IN] => India

[ES] => Spain

[DE] => Germany

)

Listing E

"United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");asort(\$data); print_r(\$data);

?>

Array ([DE] => Germany

[IN] => India

[ES] => Spain

[US] => United States

)

Listing F

"United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");arsort(\$data); print_r(\$data);

?>

Array ([US] => United States

[ES] => Spain

[IN] => India

[DE] => Germany

)

PHP有一个非常独特的排序方式，这种方式使用认知而不是使用计算规则。这种特性称为自然语言排序，当创建模糊逻辑应用软件的时候这种排序方式非常有用。下面大家可以来看看它的一个简单例子，如Listing G所示：

Listing G

natsort(\$data); print_r(\$data);?>

Array ([0] => book-1

[1] => book-10

[2] => book-100

[3] => book-5

)

Array

(

[0] => book-1

[3] => book-5

[1] => book-10

[2] => book-100

)

Listing H

?>

Array ([0] => book-100

[1] => book-10

[2] => book-5

[3] => book-1

)

PHP也能让你定义自己的排序算法，你可以通过创建你自己的比较函数，并把它传递给usort()函数。如果第一个参数比第二个参数“小”的话，比较函数必须返回一个比0小的数，如果第一参数比第二个参数“大”的话，比较函数应该返回一个比0大的数。

Listing I就是这样的一个例子，在这个例子中根据它们的长度对数组元素进行排序，最短的项放在最前面：

Listing I

print_r(\$data); function sortByLen(\$a, \$b) {

if (strlen(\$a) == strlen(\$b)) {

return 0;

} else {

return (strlen(\$a) > strlen(\$b)) ? 1 : -1;

}

}

?>

Array ([0] => jay@zoo.tw

[1] => joe@host.com

[2] => john.doe@gh.co.uk

[3] => asmithsonian@us.info

)

PHP有一个非常独特的排序方式，这种方式使用认知而不是使用计算规则。这种特性称为自然语言排序，当创建模糊逻辑应用软件的时候这种排序方式非常有用。下面大家可以来看看它的一个简单例子，如Listing G所示：

Listing G

natsort(\$data); print_r(\$data);?>

Array ([0] => book-1

[1] => book-10

[2] => book-100

[3] => book-5

)

Array

(

[0] => book-1

[3] => book-5

[1] => book-10

[2] => book-100

)

Listing H

?>

Array ([0] => book-100

[1] => book-10

[2] => book-5

[3] => book-1

)

PHP也能让你定义自己的排序算法，你可以通过创建你自己的比较函数，并把它传递给usort()函数。如果第一个参数比第二个参数“小”的话，比较函数必须返回一个比0小的数，如果第一参数比第二个参数“大”的话，比较函数应该返回一个比0大的数。

Listing I就是这样的一个例子，在这个例子中根据它们的长度对数组元素进行排序，最短的项放在最前面：

Listing I

print_r(\$data); function sortByLen(\$a, \$b) {

if (strlen(\$a) == strlen(\$b)) {

return 0;

} else {

return (strlen(\$a) > strlen(\$b)) ? 1 : -1;

}

}

?>

Array ([0] => jay@zoo.tw

[1] => joe@host.com

[2] => john.doe@gh.co.uk

[3] => asmithsonian@us.info

)

Listing J

1, "name" => "Boney M", "rating" => 3),

array("id" => 2, "name" => "Take That", "rating" => 1),

array("id" => 3, "name" => "The Killers", "rating" => 4),

array("id" => 4, "name" => "Lusain", "rating" => 3),

); foreach (\$data as \$key => \$value) {

\$name[\$key] = \$value['name'];

\$rating[\$key] = \$value['rating'];

}

array_multisort(\$rating, \$name, \$data); print_r(\$data);?>

Array ([0] => Array

(

[id] => 2

[name] => Take That

[rating] => 1

) [1] => Array

(

[id] => 1

[name] => Boney M

[rating] => 3

)

[2] => Array

(

[id] => 4

[name] => Lusain

[rating] => 3

)

[3] => Array

(

[id] => 3

[name] => The Killers

[rating] => 4

)

)

array_multisort()函数是PHP中最有用的函数之一，它有非常广泛的应用范围。另外，就如你在例子中所看到的，它能对多个不相关的数组进行排序，也可以使用其中的一个元素作为下次排序的基础，还可以对数据库结果集进行排序。

PHP对数组的某个字段排序方法1

private function arrCmp(\$a,\$b){

if(\$a['day_time'] == \$b['day_time']){

return 0;

}

return(\$a['day_time']<\$b['day_time']) ? -1 : 1;

}

usort(\$new, array("DeveloperController", "arrCmp"));

Array (

[0] => Array ( [day_time] => 1291161600 [one_sum_click_num] => 2 [two_sum_click_num] => 2 )

[1] => Array ( [day_time] => 1291248000 [one_sum_click_num] => 7 [two_sum_click_num] => 5 )

[2] => Array ( [day_time] => 1291334400 [one_sum_click_num] => 4 )

[3] => Array ( [day_time] => 1291593600 [two_sum_click_num] => 2 ) ) )

volume | edition -------+-------- 67 | 2 86 | 1 85 | 6 98 | 2 86 | 6 67 | 7

\$data[]=array('volume'=>67,'edition'=>2);

\$data[]=array('volume'=>86,'edition'=>1);

\$data[]=array('volume'=>85,'edition'=>6);

\$data[]=array('volume'=>98,'edition'=>2);

\$data[]=array('volume'=>86,'edition'=>6);

\$data[]=array('volume'=>67,'edition'=>7);

?>

//取得列的列表

foreach(\$dataas\$key=>\$row){

\$volume[\$key]=\$row['volume'];

\$edition[\$key]=\$row['edition'];

}

//将数据根据volume降序排列，根据edition升序排列

//把\$data作为最后一个参数，以通用键排序

array_multisort(\$volume,SORT_DESC,\$edition,SORT_ASC,\$data);

?>

volume | edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7