首頁  >  文章  >  後端開發  >  php的排序函數

php的排序函數

WBOY
WBOY原創
2016-07-29 09:12:051484瀏覽

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>)</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>)</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教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn