PHP array sorting

Source: Internet
Author: User

 <? Php <br/> $ arr = array (5 => "zero", 3 => "one", 4 => "two "); // define an array <br/> sort ($ ARR); // use sort to sort the array <br/> foreach ($ arr as $ key => $ value) // traverse the array to view the sorted result <br/>{< br/> echo "key: $ key; Value: $ value ;"; <br/>}< br/>?> <Br/>

<? Php <br/> // define an array <br/> $ arr = array (5 => "zero", 3 => "one ", 4 => "two"); <br/> // sort the Array Using rsort <br/> rsort ($ ARR ); <br/> // traverse the array to view the sorted result <br/> foreach ($ arr as $ key => $ value) {<br/> echo "key: $ key; Value: $ value; "; <br/>}< br/>?> <Br/>

<? Php <br/> // define two arrays <br/> $ AR1 = array (5 => "zero", 3 => "one ", 4 => "two"); <br/> $ AR2 = array (4 => "three", 9 => "four", 1 => "five "); <br/> // sort the array <br/> array_multisort ($ AR1, $ AR2 ); <br/> // traverse the array to view the sorted result <br/> foreach ($ AR1 as $ key => $ value) {<br/> echo "key: $ key; Value: $ value; "; <br/>}< br/> echo" <br> "; <br/> foreach ($ AR2 as $ key => $ value) {<br/> echo "key: $ key; Value: $ value ;"; <br/>}< br/>?> <Br/>

<? Php <br/> // define a two-dimensional array <br/> $ array = array ("AR1" => array (5 => 100, 3 => 120, 4 => 30), <br/> "AR2" => array (4 => "three", 9 => "four", 1 => "five ")); <br/> // sort arrays <br/> array_multisort ($ array ["AR1"], sort_numeric, sort_desc, <br/> $ array ["AR2"], sort_string, sort_asc); <br/> // traverse the array to view the sorted result <br/> foreach ($ array as $ V1) <br/>{< br/> foreach ($ V1 as $ V2) <br/>{< br/> echo "$ v2/N "; <br/>}< br/>?> <Br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.