Why/how does this array_multisort() work?

让人想犯罪 __ 提交于 2019-12-02 01:53:23

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!