Why/how does this array_multisort() work?

后端 未结 1 705
無奈伤痛
無奈伤痛 2021-01-22 03:01

Code:

 array(
        \'name\' => \'Unique ID 3\',
        \'count\' => 11
    ),
    \'uid1\' =         


        
相关标签:
1条回答
  • 2021-01-22 03:19

    I wasn't going to post this as an answer, but I needed just a few extra characters.

    I can't say definitively how it does do it on a technical level, but I think addressing specifically "How did array_multisort() know to apply the sorting result of $counts to the count sub-key", the answer is "it didn't". Change the sub-keys and you get the same result. Maybe I'm not reading your question the way you intended, but it seems as if you're asking how does it know to look at the sub-key values of $data and then do the sorting. But it doesn't. It looks at how $names was sorted (which looks at how $counts was sorted) and applies the same sort to the primary keys of $data. Meaning it looks at what original position was moved to what new position. The fact that your first array is made from the values of one sub-key and the second was made from another sub-key is irrelevant. Hand build those arrays in the same order and you'll get the same results.

    For the bonus, I'd say it's related to "If two members compare as equal, their relative order in the sorted array is undefined.". So it's more just coincidence than anything else.

    0 讨论(0)
提交回复
热议问题