本文介绍了维持数字索引协会在array_multisort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以排序多维数组,但没有保持数字索引关联。

我如何才能让数字索引关联?

code:

  $服务员[76] =阵列('重量'=> 67,'特色'=> 1);
$服务员[14] =阵列(重量= GT; 41'菜'=→2);
$服务员[58] =阵列(重量= GT; 85,特色菜'=→3);
$服务员[89] =阵列(重量= 98,特色菜'=→4);
$服务员[68] =阵列(重量= GT; 86,特色菜'=大于5);
$服务员[31] =阵列(重量= GT; 13'菜'=→6);
的print_r($侍应生);
//获取服务员的名单
的foreach($服务员为的$ id => $服务员){
    $权重[$ ID] = $服务员['重量'];
    $特色[$ ID] = $服务员['特色'];}//排序体重下降的数据,特产上升
//把$ data作为最后一个参数,以通用键排序
在array_multisort(
    $权重,SORT_DESC,SORT_NUMERIC,
    $特色,SORT_ASC,SORT_NUMERIC,
    $服务员
);
的print_r($侍应生);

OUTPUT:

 阵列

    [0] =>排列
        (
            [重量] => 98
            [特色] => 4
        )    [1] =>排列
        (
            [重量] => 86
            [特色] =>五
        )    [2] =>排列
        (
            [重量] => 85
            [特色] => 3
        )    [3] =>排列
        (
            [重量] => 67
            [特色] => 1
        )    [4] =>排列
        (
            [重量] => 41
            [特色] => 2
        )    [5] =>排列
        (
            [重量] => 13
            [特色] => 6
        ))

所需的输出:

 阵列

    [89] =>排列
        (
            [重量] => 98
            [特色] => 4
        )    [68] =>排列
        (
            [重量] => 86
            [特色] =>五
        )    [58] =>排列
        (
            [重量] => 85
            [特色] => 3
        )    [76] =>排列
        (
            [重量] => 67
            [特色] => 1
        )    [14] =>排列
        (
            [重量] => 41
            [特色] => 2
        )    [31] =>排列
        (
            [重量] => 13
            [特色] => 6
        ))


解决方案

  $键= array_keys($侍应生);
在array_multisort(
    $权重,SORT_DESC,SORT_NUMERIC,
    $特色,SORT_ASC,SORT_NUMERIC,
    $服务员,$键
);
$服务员= array_combine($键,$服务员);

或使用uasort

  uasort(
    $的数据,
    功能($ some_data,$ another_data){        $结果= 0;        如果($ some_data [重量]≥$ another_data ['体重']){
            $结果= -1;
        } ELSEIF($ some_data ['重量'< $ another_data ['重量']){
            $结果= 1;
        } elseif的($ some_data ['菜']≥$ another_data ['菜']){
            $结果= 2;
        } ELSEIF($ some_data ['特色'< $ another_data ['特色']){
            $结果= -2;
        }        返回$结果;    }
);

但uasort性能比在array_multisort显著恶化

I can sort a multidimensional array but without keeping the numerical index association.

How can I keep the numerical index association?

CODE:

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

OUTPUT:

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
        )

)

DESIRED OUTPUT:

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
        )

)
解决方案
$keys = array_keys($waiters);
array_multisort(
    $weight, SORT_DESC, SORT_NUMERIC,
    $specialties, SORT_ASC, SORT_NUMERIC,
    $waiters, $keys
);
$waiters = array_combine($keys, $waiters);

or use 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;

    }
);

but the uasort performance is significantly worse than array_multisort

这篇关于维持数字索引协会在array_multisort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:35