当前位置: 首页 > 知识库问答 >
问题:

array_multisort维护数字索引关联[重复]

蒙麒
2023-03-14

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

怎样才能保持数值索引关联?

法典:

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

)

共有2个答案

吕高昂
2023-03-14

对于所需的输出,请使用以下代码

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

?>
章安宜
2023-03-14
$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差

 类似资料:
  • 问题内容: 我有一个数组: 我尝试使用PHP进行各种功能来实现此数组结构: 它由数组值和键/值保持的顺序。有任何想法吗? 问题答案: 这应该使用asort()起作用: 输出:

  • 问题内容: 我有一个关联数组,我需要找到一个键的数字位置。我可以手动遍历数组以找到它,但是有没有更好的方法内置到PHP中呢? 问题答案:

  • 问题内容: 我有一个使用该函数构建的数组,但是看到我如何将其与随机/动态数据一起使用,我看到索引不断变化: 我需要始终从0开始对数组进行排序。我正在测试不同的数据,有时它从0开始,而在其他测试中,它从不同的数字开始。我进行了研究,发现数组从零开始,但似乎仅适用于该用户的特定情况。 我怎样才能做到这一点? 问题答案: $your_new_array = array_values($your_old_

  • 问题内容: 我有一个索引元组数组,我想用它从多维numpy数组中选取值, 理解只有在已知的情况下才有效。 有什么提示吗? 问题答案: 您可以将的转置版本转换为元组,然后为矢量化解决方案建立索引-

  • 问题内容: 有以下查询结果:(key1和key2可以是任何文本) 我希望将数据存储在网格(可能是数组)中,像这样 循环 所有记录: 在PHP中,使用关联数组非常容易: 但是在JavaScript这样的关联数组中不起作用。阅读大量教程之后,我所能得到的就是: 但不起作用。我尝试了对象数组,但是对象属性不能是自由文本。我阅读教程的时间越长,我就越困惑。 任何想法都欢迎:) 问题答案: 只需使用常规的J

  • 本文向大家介绍php中array_multisort对多维数组排序的方法,包括了php中array_multisort对多维数组排序的方法的使用技巧和注意事项,需要的朋友参考一下 PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。但是多维数组的格式要一致 关联(string)键名保持不变,但数字键名会被重新索引。 输入数组被当成一个表的列