Php array descending order detailed tutorial

Source: Internet
Author: User
Tags arrays
<? Php
$ People = array ('name', 'sex', 'nation', 'birth ');
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
Sort ($ people );
Echo "<br/> --- sorted --- <br/> ";
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
?>

The array elements in ascending order are displayed as birth name nation sex. Of course, the sort () function is case-sensitive (the order of letters from large to small is:... Z... A... Z)

The sort () function also has a second parameter. The ascending rule is used to compare numbers or strings.

 

<? Php
Echo "--- sort by numbers in ascending order --- <br/> ";
$ Num2 = array ('26', '3 ',);
Sort ($ num2, sort_numeric );
Foreach ($ num2 as $ mychrs)
Echo $ mychrs ."";
Echo "<br/> --- sort by character in ascending order --- <br/> ";
$ Num3 = array ('26', '3 ');
Sort ($ num3, sort_string );
Foreach ($ num3 as $ mychrs)
Echo $ mychrs ."";
?>


Sorting of associated arrays in php

In addition to numeric index arrays, php also supports related arrays. For example, the following array is a correlated array:

$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');

By default, sort ($ sort les) is sorted in ascending order based on the defined values of the elements. You can use the asort () function to represent the associated arrays, in the joined array, the most important thing is to sort by keywords (such as xm, xb, mz, etc.) in ascending order. This method uses the ksort () function.

<? Php
$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
Foreach ($ minutes Les as $ mychrs)
Echo $ mychrs ."";
Echo "<br/> -- Sort By element values in ascending order -- <br/> ";
Asort ($ minutes les );
Foreach ($ minutes Les as $ mychrs)
Echo $ mychrs ."";
Echo "<br/> -- sort by keywords in ascending order -- <br/> ";
Ksort ($ cmdles );
Foreach ($ minutes Les as $ mychrs)
Echo $ mychrs ."";
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.