php的排序函数

WBOY
Lepaskan: 2016-07-29 09:12:05
asal
1482 orang telah melayarinya

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>
Salin selepas log masuk

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>
Salin selepas log masuk

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>
Salin selepas log masuk

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>
Salin selepas log masuk

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>
Salin selepas log masuk

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>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan