为什么/这个array_multisort()如何工作? [英] Why/how does this array_multisort() work?

查看:165
本文介绍了为什么/这个array_multisort()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

<?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 );

输出:

// $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
        )

)


问题:

array_multisort()如何知道将$counts的排序结果应用于多维数组$data中每个项目的count子项?

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?

$names子键的$names

Likewise for $names to the name sub-key.

奖金:

如果在函数调用中未注释$names, SORT_ASC, SORT_STRING,,则uid3仍在uid1之后结束.为什么?

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

php array_multisort如何工作?

如何对类似于mysql的数组进行排序

推荐答案

我不打算将其发布为答案,但是我只需要几个额外的字符.

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

我不能确切地说它在技术水平上是怎么做的,但是我认为要特别解决"array_multisort()是如何知道将$counts的排序结果应用于计数的?子键",答案是没有".更改子项,您将获得相同的结果.也许我不是按照您的预期来阅读您的问题,但似乎您是在询问如何查看$data的子键值然后进行排序.但事实并非如此.它查看$names的排序方式(查看$counts的排序方式)并将相同的排序应用于$data primary 键.这意味着它将查看将原来的位置移到了新的位置.您的第一个数组是由一个子键的值组成的,而第二个数组是由另一个子键的值组成的事实是无关紧要的.以相同的顺序手动构建这些数组,您将获得相同的结果.

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.

这篇关于为什么/这个array_multisort()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆