Ask a question about the sort of PHP

Source: Internet
Author: User
I want to sort the arrays numerically, with the following code:

$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);asort($a, SORT_NUMERIC);var_dump($a);

The printing results are as follows:

array(5) {  'd' =>  int(10)  'a' =>  int(10)  'c' =>  int(10)  'z' =>  int(10)  'b' =>  int(11)}

Problem:

    • I need to sort the values (implemented)
    • If the values are the same, the order position of the array keys does not change, how do I do

Example:
The above array

$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);

After sorting, the order of the keys is

D = ten, a = ten, c = ten, z = ten, B = 11

I want to be able to implement the order for

A = ten, z = ten, c = ten, d = ten, B = 11

Reply content:

I want to sort the arrays numerically, with the following code:

$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);asort($a, SORT_NUMERIC);var_dump($a);

The printing results are as follows:

array(5) {  'd' =>  int(10)  'a' =>  int(10)  'c' =>  int(10)  'z' =>  int(10)  'b' =>  int(11)}

Problem:

    • I need to sort the values (implemented)
    • If the values are the same, the order position of the array keys does not change, how do I do

Example:
The above array

$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);

After sorting, the order of the keys is

D = ten, a = ten, c = ten, z = ten, B = 11

I want to be able to implement the order for

A = ten, z = ten, c = ten, d = ten, B = 11

Http://3v4l.org/fsQkS

function hasort($arr) {    $a = $b = array();    foreach($arr as $v => $k) $a[$k][] = $v;    ksort($a);    foreach($a as $v => $i)         foreach($i as $k) $b[$k] = $v;    return $b;}$a = array('a' => 10, 'z' => 10, 'c' => 10, 'b' => 11, 'd' => 10);$a = hasort($a);print_r($a);/**Array(    [a] => 10    [z] => 10    [c] => 10    [d] => 10    [b] => 11)**/
  • 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.