Home >Backend Development >PHP Tutorial >php sorting function

php sorting function

WBOY
WBOYOriginal
2016-07-29 09:12:051515browse

sort(): Simply sort the elements of an array from low to high. This function can be arranged either numerically or alphabetically.

<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(): Sort the array elements from high to low, either numerically or alphabetically.

<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(): A function that sorts according to keywords. At the same time, it maintains the relevance of keywords during the sorting process.

<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(): Reverse the array according to keywords.

<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(): Use value sorting instead of keyword sorting. Please note that the relationship between keywords and values ​​will always be maintained.

<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(): Sort in alphabetical order by value.

<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); }); });

The above introduces the sorting function of PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn