Interpreting PHP array sorting

WBOY
Release: 2016-07-28 08:27:29
Original
1247 people have browsed it

PHP has a set of powerful function libraries. Let’s take a look at the specific usage of each array sorting function. The screenshots below are from the PHP7.0 manual.
 解读PHP数组排序


  • sort( ) — Sort the array from low to high. This function is the root function of all sorting functions; bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )Two parameters, in addition to the array, the second optional parameter sort_flags changes the sorting through optional type flags Behavior.
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"bens2"</span>,<span>"honda3"</span>,<span>"BMW4"</span>,<span>"aens1"</span>,<span>"BMW5"</span>);
sort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
{
    <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>;
    <span>//输出auto[0]=BMW4 auto[1]=BMW5 auto[2]=aens1 auto[3]=bens2 auto[4]=honda3</span>
}</span></span></code>
Copy after login
  • rsort — Sort the array in reverse, the parameter list is the same as sort, and the return value is the same as
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"bens2"</span>,<span>"honda3"</span>,<span>"BMW4"</span>,<span>"aens1"</span>,<span>"BMW5"</span>);
rsort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
{
    <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>;
    <span>//输出auto[0]=honda3 auto[1]=BMW5 auto[2]=BMW4 auto[3]=bens2 auto[4]=aens1</span>
}</span></span></code>
Copy after login

  • asort() — Sort the array and maintain the index relationship, the parameters and return value are the same as above, the explanation in the manual is "This function sorts the array, and the index of the array remains associated with the unit. It is mainly used to sort associative arrays where the order of the units is important." Personally, I think it and sort() focus on sorting associative arrays. Simply put, the sorting rules are based on values, not keys. This is easily distinguished from ksort().
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"a"</span>=><span>"bens2"</span>,<span>"b"</span>=><span>"honda3"</span>,<span>"c"</span>=><span>"BMW4"</span>,<span>"d"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>);
asort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
{
    <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>;
    <span>//输出auto[d]=aens1 auto[a]=bens2 auto[c]=BMW4 auto[e]=BMW5 auto[b]=honda3</span>
}</span></span></code>
Copy after login
  • arsort() — Reverse sort the array and maintain index relationship, it is the inverse sort of asort().
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"a"</span>=><span>"bens2"</span>,<span>"b"</span>=><span>"honda3"</span>,<span>"c"</span>=><span>"BMW4"</span>,<span>"d"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>);
arsort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
{
    <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>;
    <span>//输出auto[b]=honda3 auto[e]=BMW5 auto[c]=BMW4 auto[a]=bens2 auto[d]=aens1</span>
}</span></span></code>
Copy after login

  • ksort() - Sort the array by key name. The manual explains that "sort the array by key name and retain the association between key name and data. This function is mainly used for associative arrays." The parameters and return value are the same as sort ()
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"a"</span>=><span>"honda3"</span>,<span>"d"</span>=><span>"BMW4"</span>,<span>"c"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>);
ksort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
{
    <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>;
    <span>//输出auto[a]=honda3 auto[b]=bens2 auto[c]=aens1 auto[d]=BMW4 auto[e]=BMW5</span>
}</span></span></code>
Copy after login
Copy after login
  • krsort — Sort the array in reverse order by key name. The parameters and return values ​​are the same as above.
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"a"</span>=><span>"honda3"</span>,<span>"d"</span>=><span>"BMW4"</span>,<span>"c"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>);
ksort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
{
    <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>;
    <span>//输出auto[a]=honda3 auto[b]=bens2 auto[c]=aens1 auto[d]=BMW4 auto[e]=BMW5</span>
}</span></span></code>
Copy after login
Copy after login

  • natsort — Use the "natural sorting" algorithm to sort the array. The manual explains that "This function implements a sum People usually sort alphanumeric strings using the same sorting algorithm and retaining the original key/value association. This is called "natural sorting" and is different from the sorting method of sort() which takes only one parameter. That is, the parameters passed in.

shuffle() — shuffles the array, it will delete the key names, not just the key names. Its parameters are only the array variables passed in, and the return value is true or false
  • <code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"e"</span>=><span>"bens20"</span>,<span>"a"</span>=><span>"bens31"</span>,<span>"d"</span>=><span>"bens41"</span>,<span>"c"</span>=><span>"bens1"</span>,<span>"e"</span>=><span>"bens15"</span>);
    sort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>echo</span><span>"sort排序:<br>"</span>;
    <span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>)
    {
        <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"<br>"</span>;
    
    }
    <span>$auto1</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"e"</span>=><span>"bens20"</span>,<span>"a"</span>=><span>"bens31"</span>,<span>"d"</span>=><span>"bens41"</span>,<span>"c"</span>=><span>"bens1"</span>,<span>"e"</span>=><span>"bens15"</span>);
    natsort(<span>$auto1</span>);<span>//参数只有一个</span><span>echo</span><span>"natsort排序:<br>"</span>;
    <span>foreach</span>(<span>$auto1</span><span>as</span><span>$key</span>=><span>$val</span>)
    {
        <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"<br>"</span>;
    
    }
    <span>//输出</span><span>/* sort排序:
    auto[0]=bens1
    auto[1]=bens15
    auto[2]=bens2
    auto[3]=bens31
    auto[4]=bens41
    natsort排序:
    auto[c]=bens1
    auto[b]=bens2
    auto[e]=bens15
    auto[a]=bens31
    auto[d]=bens41 */</span></span></span></code>
    Copy after login
  • ').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); }); The above has introduced the interpretation of PHP array sorting, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template