Generally, all elements in an array are represented by characters or numbers. Therefore, you can sort the array elements in ascending order. This function is sort (), for example: & lt ;? Php $ learn learray (& amp; 39; name & amp; 39;, & amp; 39
Generally, all elements in an array are represented by characters or numbers. Therefore, you can sort the array elements in ascending order. This function is sort (), for example:
-
- $ People = array ('name', 'sex', 'nation', 'birth ');
- Foreach ($ people as $ mychrs)
- Echo $ mychrs ."";
- Sort ($ people );
- Echo" --- After sorting --- ";
- 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. In general, the ascending rule is used to compare numbers or strings:
-
- Echo "--- sort by numbers in ascending order --- ";
- $ Num2 = array ('26', '3 ',);
- Sort ($ num2, sort_numeric );
- Foreach ($ num2 as $ mychrs)
- Echo $ mychrs ."";
- Echo" --- Sort by character in ascending order --- ";
- $ 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.
-
- $ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
- Foreach ($ minutes Les as $ mychrs)
- Echo $ mychrs ."";
- Echo" -- Sort By element values in ascending order -- ";
- Asort ($ minutes les );
- Foreach ($ minutes Les as $ mychrs)
- Echo $ mychrs ."";
- Echo" -- Sort by keywords in ascending order -- ";
- Ksort ($ cmdles );
- Foreach ($ minutes Les as $ mychrs)
- Echo $ mychrs ."";
- ?>