【问题标题】:array_multisort with maintaining numeric index association [duplicate]维护数字索引关联的array_multisort [重复]
【发布时间】:2014-05-19 14:46:15
【问题描述】:

我可以对多维数组进行排序,但不保持数字索引关联。

如何保持数字索引关联?

代码:

$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
  • 改为创建您自己的用户函数,这将解决您的问题并使问题相同。
  • @vogomatix 请检查问题编辑。老问题是让它重复。现在它不应该重复。
  • 你没有抓住重点——问题的答案仍然是创建你自己的比较函数并使用链接问题所暗示的 uasort,仅仅因为存在 anarray_multisort 方法并不一定意味着你 必须使用它。
  • @SazzadHossainKhan 如果您阅读过 array_multisort 的文档,您就会知道无法保留索引。它清楚地说明,我引用:'关联(字符串)键将被保留,但数字键将被重新索引。'

标签: php arrays array-multisort


【解决方案1】:
$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 差

【讨论】:

    【解决方案2】:

    对于您想要的输出,请使用以下代码:

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

    【讨论】:

    • 或者你可以简单地在这里发布代码。
    • 详细解释我如何在这里发布我的代码
    • 当有帮助部分时,我为什么要这样做? stackoverflow.com/editing-help#code
    • 为什么我必须遵守所有的编码标准?我只是在这里为他们想要的人提供更好的解决方案。看看我为他们做了什么。所以不要因为你的错误意见而让他们失望。
    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多