php的排序函数

WBOY
发布: 2016-07-29 09:12:05
原创
1482 人浏览过

sort(): 将一个数组元素从低到高进行简单排序,这个函数既可以按数字大小排列也可以按字母顺序排列。

<code>Listing A

<span><?php </span><span>$data</span> = <span>array</span>(<span>5</span>,<span>8</span>,<span>1</span>,<span>7</span>,<span>2</span>);

  sort(<span>$data</span>);

  print_r(<span>$data</span>);

<span>?></span>输出结果如下所示:

<span>Array</span> ([<span>0</span>] => <span>1</span>[<span>1</span>] => <span>2</span>[<span>2</span>] => <span>5</span>[<span>3</span>] => <span>7</span>[<span>4</span>] => <span>8</span>)</span></code>
登录后复制

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

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

<span>Array</span> ([<span>0</span>] => <span>8</span>[<span>1</span>] => <span>7</span>[<span>2</span>] => <span>5</span>[<span>3</span>] => <span>2</span>[<span>4</span>] => <span>1</span>)</span></code>
登录后复制

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

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

<span>Array</span> ([DE] => Germany

[ES] => Spain

[IN] => India

[US] => United States

)</span></code>
登录后复制

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

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

<span>Array</span> ([US] => United States

[IN] => India

[ES] => Spain

[DE] => Germany

)</span></code>
登录后复制

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

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

<span>Array</span> ([DE] => Germany

[IN] => India

[ES] => Spain

[US] => United States

)</span></code>
登录后复制

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

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

[ES] => Spain

[IN] => India

[DE] => Germany

)</span></code>
登录后复制
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板