Example of sort, asort, and ksort usage in PHP array sorting, asortksort_PHP tutorial

Source: Internet
Author: User
PHP array sorting-sort, asort, and ksort usage instances, asortksort. The sort, asort, and ksort usage examples of PHP array sorting. asortksort this article describes the usage of sort, asort, and ksort in PHP array sorting for your reference. The following is an example of sort, asort, and ksort usage in PHP array sorting. asortksort

This article describes the usage of sort, asort, and ksort in PHP array sorting for your reference. The specific example is as follows:

<? Php $ arr = array ('D' => 'sdf ', 'R' => 'sdf', 'a' => 'Eee '); // sort ($ arr); // sorts the values of the array, deletes the previous key value, and changes it to the index array // asort ($ arr ); // sorts the array by value and maintains the index relationship. ksort ($ arr) applies to the index array and associated array; // sorts the array by key value, and maintain the index relationship, both the index array and the associated array are applicable // The corresponding reverse order and rsort arsort krsort // compare the usort uksort uasort. The second parameter is the comparison function. The first parameter must be equal to or greater than the second parameter. return a floating point number equal to or greater than 0. only the integer part print_r ($ arr);?>

The examples in this article only demonstrate the usage of ksort. if you are interested, you can test the running results of sort and asort to gain a better understanding of them. I hope this example will help you with PHP programming.


Php uses the asort and ksort functions to sort array elements.

I don't know how you wrote it. it's missing planet1, but you don't need it. you can just use this 2. In addition, use commas (,) to separate the array blocks, instead of semicolons, use a semicolon to end the statement. below is my change. let's see for yourself ..
$ Planet2 = array (
'X' => 'global ',
'Y' => 'Venus ',
'Z' => 'Mars ',
'A' => 'Jupiter ',
'B' => 'Saturn ',
);

Asort ($ planet2 );

Echo 'use the function asort to sort array elements :';
Echo' ';

Foreach ($ planet2 as $ key => $ value)
{
Echo 'planet2 ['. $ key.'] = '. $ value;
Echo' ';
Echo' ';
}

Echo' ';
Echo 'use the ksort function to sort array elements :';
Echo' ';
Ksort ($ planet2 );
Foreach ($ planet2 as $ key => $ value)
{
Echo 'planet2 ['. $ key.'] = '. $ value;
Echo' ';
Echo' ';
}
?>

How can php arrays be sorted by letters and numbers in ascending order?

This should be the result of sort that comes with a1, a12, a2, b1 php.
It is a character ratio.
You have to think of your number as a whole.

Just make the array look like this
For example
Array ('A1', 'A2 ', 'a11', 'b1', 'b11 ')
Processed
Array (
'A' => array (
1 => array ('A1 '),
2 => array ('A2 '),
11 => array ('a11 ')

),
'B' => array (
1 => array ('b1 '),
11 => array ('b11 ')

)

)

Examples in this article describes how to use sort, asort, and ksort in PHP array sorting for your reference. The specific instance is as follows...

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.