Why/how does this array_multisort() work?

末鹿安然 提交于 2019-12-02 06:02:18

问题


Code:

<?php
$data = array(
    'uid3' => array(
        'name' => 'Unique ID 3',
        'count' => 11
    ),
    'uid1' => array(
        'name' => 'Unique ID 1',
        'count' => 11
    ),
    'uid2' => array(
        'name' => 'Unique ID 2',
        'count' => 15
    ),
    'uid4' => array(
        'name' => 'Unique ID 4',
        'count' => 13
    )
);

$counts = array_map( function( $v ){
    return $v[ 'count' ];
}, $data );

$names = array_map( function( $v ){
    return $v[ 'name' ];
}, $data );

print_r( $counts );
print_r( $names );

array_multisort(
    $counts, SORT_DESC, SORT_NUMERIC,
    $names, SORT_ASC, SORT_STRING,
    $data
);

print_r( $data );

Output:

// $counts
Array
(
    [uid3] => 11
    [uid1] => 11
    [uid2] => 15
    [uid4] => 13
)

// $names
Array
(
    [uid3] => Unique ID 3
    [uid1] => Unique ID 1
    [uid2] => Unique ID 2
    [uid4] => Unique ID 4
)

// $data after sorting
Array
(
    [uid2] => Array
        (
            [name] => Unique ID 2
            [count] => 15
        )

    [uid4] => Array
        (
            [name] => Unique ID 4
            [count] => 13
        )

    [uid1] => Array
        (
            [name] => Unique ID 1
            [count] => 11
        )

    [uid3] => Array
        (
            [name] => Unique ID 3
            [count] => 11
        )

)

Question:

How did array_multisort() know to apply the sorting result of $counts to the count sub-key of each item in the multi-dimensional array $data?

Likewise for $names to the name sub-key.


Bonus:

If $names, SORT_ASC, SORT_STRING, is commented out of the function call then uid3 still ends up after uid1. Why?


I've been reading the following resources but I cannot wrap my mind as to how this applies to my example:

http://php.net/manual/en/function.array-multisort.php

how php array_multisort work?

how to sort array like mysql


回答1:


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.



来源:https://stackoverflow.com/questions/51385519/why-how-does-this-array-multisort-work

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