array_multisort无法正常工作?

时间:2016-11-23 09:19:32

标签: php array-multisort

我一直在使用array_multisort,但这次它确实无法正确排序。

我用它来对多维数组($ data)进行排序,但我简化了这个例子中的问题:

    $data = array(6 => 'WEEK 48', 7 => 'WEEK 49', 8 => 'WEEK 47', 9 => 'WEEK 50', 10 => 'WEEK 51');

    $sort = array(8 => 201647, 6 => 201648, 7 => 201649, 9 => 201650, 10 => 201651);

    array_multisort($sort, SORT_ASC, $data);

    Output:
    Array
    (
        [0] => WEEK 48
        [1] => WEEK 49
        [2] => WEEK 47
        [3] => WEEK 50
        [4] => WEEK 51
    )

我错过了什么?

3 个答案:

答案 0 :(得分:1)

试试这个

mode

输出:

    <?php

       $data = array(6 => 'WEEK 48', 7 => 'WEEK 49', 8 => 'WEEK 47', 9 => 'WEEK 50', 10 => 'WEEK 51');

        $sort = array(8 => 201647, 6 => 201648, 7 => 201649, 9 => 201650, 10 => 201651);

        array_multisort($data, SORT_ASC, $sort);

         echo '<pre>';
         print_r($data);

   ?>

答案 1 :(得分:0)

<强>语法

array_multisort(array1,sorting order,sorting type,array2,array3...)

您应该为您的数组提供sorting_type

array_multisort($sort, SORT_ASC, SORT_STRING, $data, SORT_ASC, SORT_NUMERIC);

var_dump($sort);
var_dump($data);

结果:

array(5) { 
    [0]=> int(201647) 
    [1]=> int(201648) 
    [2]=> int(201649) 
    [3]=> int(201650) 
    [4]=> int(201651) 
} 

array(5) { 
    [0]=> string(7) "WEEK 47" 
    [1]=> string(7) "WEEK 48" 
    [2]=> string(7) "WEEK 49" 
    [3]=> string(7) "WEEK 50" 
    [4]=> string(7) "WEEK 51" 
}

答案 2 :(得分:0)

愚蠢的我,行之间有一个ksort($data),这搞砸了排序。