欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php的排序函数

程序员文章站 2022-04-28 15:04:27
...
sort(): 将一个数组元素从低到高进行简单排序,这个函数既可以按数字大小排列也可以按字母顺序排列。
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)

rsort(): 对数组元素进行从高到低的倒排,同样可以按数字大小排列也可以按字母顺序排列。

$data = array(5,8,1,7,2);rsort($data); print_r($data);
?>它的输出结果如下:

Array ([0] => 8[1] => 7[2] => 5[3] => 2[4] => 1)

ksort(): 根据关键字进行排序的函数,同时,它在排序的过程中会保持关键字的相关性。

$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
    ksort($data);
    print_r($data);
?>它的输出结果如下:

Array ([DE] => Germany

[ES] => Spain

[IN] => India

[US] => United States

)

krsort(): 根据关键字对数组进行倒排。

$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
    krsort($data);
    print_r($data);
?>它的输出结果如下:

Array ([US] => United States

[IN] => India

[ES] => Spain

[DE] => Germany

)

asort(): 使用值排序来取代关键字排序,请注意关键字-值之间的联系会始终保持。

$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
    asort($data);
    print_r($data);
?>它的输出结果如下:

Array ([DE] => Germany

[IN] => India

[ES] => Spain

[US] => United States

)

arsort(): 根据值按字母表顺序进行倒排。

$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
    arsort($data);
    print_r($data);
?>Array ([US] => United States

[ES] => Spain

[IN] => India

[DE] => Germany

)
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php的排序函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。