Examples of sort, asort and ksort usages of php array sorting, asortksort_php tutorial

Source: Internet
Author: User
Tags array sort

Examples of sort, asort, and ksort usages of PHP arrays, Asortksort


This paper explains the use of sort, asort and ksort in the ordering of PHP arrays for reference. The specific examples are as follows:

<?php$arr = Array (' d ' = ' sdf ', ' r ' = ' sdf ', ' a ' = ' eee ');//sort ($arr);  The values of the array are re-//asort, the previous key values are deleted, and the indexed array is changed to the index ($ARR); Arrays are re-sorted by value, and index relationships are maintained, and both indexed and associative arrays are applicable to Ksort ($arr); Arrays are re-sorted by key values and indexed, indexed and associative arrays are available//correspond to reverse order and Rsort Arsort krsort//use function compare Usort uksort uasort  The second parameter is a comparison function A value that returns equal to or less than 0 when the first parameter is equal less than the second parameter will only take the integer part Print_r ($arr); >

In this example, only ksort usage is demonstrated, and interested readers can then test the results of sort and asort in order to deepen their impressions and grasp them firmly. I hope the example of this article is helpful to PHP program design.


PHP uses function Asort and ksort to sort array elements

Do not know how you write, is less a planet1, but do not have it, you can use this 2, there is the array that block with a comma, do not use semicolons, the end of the statement with a semicolon. Here's my change.
$planet 2=array (
' X ' = ' Earth ',
' Y ' = ' Venus ',
' Z ' = ' Mars ',
' A ' = ' Jupiter ',
' B ' = ' Saturn ',
);

Asort ($planet 2);

Echo ' uses the function Asort to sort the array elements: ';
Echo ' ';

foreach ($planet 2 as $key = $value)
{
Echo ' planet2['. $key. ' = '. $value;
Echo ' ;
Echo ' ;
}

Echo ' ;
Echo ' uses the function Ksort to sort the array elements: ';
Echo ' ;
Ksort ($planet 2);
foreach ($planet 2 as $key = $value)
{
Echo ' planet2['. $key. ' = '. $value;
Echo ' ;
Echo ' ;
}
?>

How do PHP arrays be sorted in ascending alphabetical order and ascending by number?

A1,a12,a2,b1 PHP comes with the sort that should be the result.
He is a transmitting of a word.
If you think of your number as a whole, you have to do it yourself.

Just make the array look like this.
For example
Array (' A1 ', ' A2 ', ' A11 ', ' B1 ', ' B11 ')
Processed into
Array
' A ' =>array (
1=>array (' A1 '),
2=>array (' A2 '),
11=>array (' A11 ')

),
' B ' =>array (
1=>array (' B1 '),
11=>array (' B11 ')

)

)

http://www.bkjia.com/PHPjc/875871.html www.bkjia.com true http://www.bkjia.com/PHPjc/875871.html techarticle php Array Sort, asort and ksort usages, asortksort This example explains the use of sort, asort, and Ksort in the ordering of PHP arrays for reference. Specific examples are as follows ...

  • Related Article

    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.