使用array_multisort同时保持数字索引关联

7
我可以对多维数组进行排序,但不保留数字索引的关联性。
如何保留数字索引关联性?
代码:
$waiters[76] = array('weight' => 67, 'specialties' => 1);
$waiters[14] = array('weight' => 41, 'specialties' => 2);
$waiters[58] = array('weight' => 85, 'specialties' => 3);
$waiters[89] = array('weight' => 98, 'specialties' => 4);
$waiters[68] = array('weight' => 86, 'specialties' => 5);
$waiters[31] = array('weight' => 13, 'specialties' => 6);
print_r($waiters);
// Obtain a list of waiters
foreach ($waiters as $id => $waiter) {
    $weight[$id]        = $waiter['weight'];
    $specialties[$id]   = $waiter['specialties'];

}

// Sort the data with weight descending, specialties ascending
// Add $data as the last parameter, to sort by the common key
array_multisort(
    $weight, SORT_DESC, SORT_NUMERIC, 
    $specialties, SORT_ASC, SORT_NUMERIC, 
    $waiters
);
print_r($waiters);

输出:

Array
(
    [0] => Array
        (
            [weight] => 98
            [specialties] => 4
        )

    [1] => Array
        (
            [weight] => 86
            [specialties] => 5
        )

    [2] => Array
        (
            [weight] => 85
            [specialties] => 3
        )

    [3] => Array
        (
            [weight] => 67
            [specialties] => 1
        )

    [4] => Array
        (
            [weight] => 41
            [specialties] => 2
        )

    [5] => Array
        (
            [weight] => 13
            [specialties] => 6
        )

)

期望的输出:

Array
(
    [89] => Array
        (
            [weight] => 98
            [specialties] => 4
        )

    [68] => Array
        (
            [weight] => 86
            [specialties] => 5
        )

    [58] => Array
        (
            [weight] => 85
            [specialties] => 3
        )

    [76] => Array
        (
            [weight] => 67
            [specialties] => 1
        )

    [14] => Array
        (
            [weight] => 41
            [specialties] => 2
        )

    [31] => Array
        (
            [weight] => 13
            [specialties] => 6
        )

)

@Naruto 可能的重复是使用 usort,但我正在使用 array_multisort - itsazzad
3
创建您自己的用户函数,这将解决您的问题并使问题保持不变。 - vogomatix
@vogomatix 请检查问题的编辑。旧问题使其重复。现在它不应该是重复的。 - itsazzad
1
你没有理解问题的关键 - 解决问题的答案仍然是创建自己的比较函数并像链接的问题建议的那样使用uasort。仅仅因为存在一个array_multisort方法并不一定意味着你必须使用它。 - vogomatix
@SazzadHossainKhan 如果你读过array_multisort的文档,你就会知道你不能保留索引。它明确说明了:'关联(字符串)键将被维护,但数字键将被重新索引。' - Naruto
显示剩余2条评论
2个回答

12
$keys = array_keys($waiters);
array_multisort(
    $weight, SORT_DESC, SORT_NUMERIC,
    $specialties, SORT_ASC, SORT_NUMERIC,
    $waiters, $keys
);
$waiters = array_combine($keys, $waiters);

或者使用uasort函数

uasort(
    $data,
    function ($some_data, $another_data) {

        $result = 0;

        if ($some_data['weight'] > $another_data['weight']) {
            $result = -1;
        } elseif ($some_data['weight'] < $another_data['weight']) {
            $result = 1;
        } elseif ($some_data['specialties'] > $another_data['specialties']) {
            $result = 2;
        } elseif ($some_data['specialties'] < $another_data['specialties']) {
            $result = -2;
        }

        return $result;

    }
);

但是 uasort 的性能明显比 array_multisort 差。


1

为了得到您想要的输出,请使用此代码:

<?php
$waiters[76] = array('weight' => 67, 'specialties' => 1);
$waiters[14] = array('weight' => 41, 'specialties' => 2);
$waiters[58] = array('weight' => 85, 'specialties' => 3);
$waiters[89] = array('weight' => 98, 'specialties' => 4);
$waiters[68] = array('weight' => 86, 'specialties' => 5);
$waiters[31] = array('weight' => 13, 'specialties' => 6);

//ksort($waiters);
//$waiters = array_reverse($waiters, true);
print_r($waiters);
// Obtain a list of waiters
foreach($waiters as $id=>$w){
    $w[$id] = $w['weight'];
}
    foreach ($waiters as $ii => $va) {
        $sorter[$ii] = $va['weight'];

    }

    natcasesort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii] = $waiters[$ii];
    }

    echo "<pre>";
    $ret = array_reverse($ret, true);
    print_r($ret);

?>

1
或者你可以直接在这里发布代码。 - deceze
请详细说明如何在这里发布我的代码。 - dhruv jadia
1
为什么需要这样做呢?既然有帮助部分可以解决它。 http://stackoverflow.com/editing-help#code - deceze
为什么我必须遵循所有编码标准?我只是在这里为人们提供更好的解决方案,以满足他们的需求。看看我为他们所做的事情。因此,请不要让你的错误观点让他们失望。 - dhruv jadia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接