1

我一直在使用 array_multisort 函数,但我正在努力让它与我拥有的数组一起工作。

这是我要排序的多维数组的 var 转储:

array(2) { 
[1]=> array(6) { 
    ["totalprice"]=> float(103.32)
    ["itemsprice"]=> float(83.33) 
    ["deliveryprice"]=> float(19.99)
    ["qualityscore"]=> int(100)
    ["reliabilityscore"]=> int(100)
    ["itemtemplates"]=> array(4) {
         [1]=> array(3) { 
            ["price"]=> float(374)
            ["qty"]=> int(200)
            ["name"]=> string(17) "English A2 Poster"
        }
        [3]=> array(3) { 
            ["price"]=> float(374)
            ["qty"]=> int(500)
            ["name"]=> NULL
        }
        [6]=> array(3) {
            ["price"]=> float(83.333333333333)
            ["qty"]=> int(100)
            ["name"]=> string(16) "French A3 Poster"
        }
        [5]=> array(3) {
            ["price"]=> float(83.333333333333)
            ["qty"]=> int(5000) ["name"]=> NULL
        }
    }
}
[2]=> array(6) { 
    ["totalprice"]=> float(103.32)
    ["itemsprice"]=> float(83.33) 
    ["deliveryprice"]=> float(19.99)
    ["qualityscore"]=> int(80)
    ["reliabilityscore"]=> int(100)
    ["itemtemplates"]=> array(4) {
         [1]=> array(3) { 
            ["price"]=> float(374)
            ["qty"]=> int(200)
            ["name"]=> string(17) "English A2 Poster"
        }
        [3]=> array(3) { 
            ["price"]=> float(374)
            ["qty"]=> int(500)
            ["name"]=> NULL
        }
        [6]=> array(3) {
            ["price"]=> float(83.333333333333)
            ["qty"]=> int(100)
            ["name"]=> string(16) "French A3 Poster"
        }
        [5]=> array(3) {
            ["price"]=> float(83.333333333333)
            ["qty"]=> int(5000) ["name"]=> NULL
        }
    }
}
[3]=> array(6) { 
    ["totalprice"]=> float(83.32)
    ["itemsprice"]=> float(63.33) 
    ["deliveryprice"]=> float(19.99)
    ["qualityscore"]=> int(60)
    ["reliabilityscore"]=> int(40)
    ["itemtemplates"]=> array(4) {
         [1]=> array(3) { 
            ["price"]=> float(374)
            ["qty"]=> int(200)
            ["name"]=> string(17) "English A2 Poster"
        }
        [3]=> array(3) { 
            ["price"]=> float(374)
            ["qty"]=> int(500)
            ["name"]=> NULL
        }
        [6]=> array(3) {
            ["price"]=> float(83.333333333333)
            ["qty"]=> int(100)
            ["name"]=> string(16) "French A3 Poster"
        }
        [5]=> array(3) {
            ["price"]=> float(83.333333333333)
            ["qty"]=> int(5000) ["name"]=> NULL
        }
    }
}

}

我需要按总价格 ASC 排序,然后按质量得分 DESC 排序。

我尝试了以下方法:

$sorted = array_multisort($array['totalprice'], SORT_ASC, SORT_NUMERIC,
    $array['qualityscore'], SORT_NUMERIC, SORT_DESC);

不幸的是,这不起作用。有没有人更精通这个功能并且可能知道我哪里出错了?或者如果有一个简单的替代这个功能?

提前致谢!

4

2 回答 2

3

使用该usort()功能。

您显然需要为它编写自己的排序函数。将该函数传入usort()以按您需要的任何标准进行排序。

有关如何定义排序功能的信息,请参阅上面链接的手册页。

于 2013-01-09T12:32:24.867 回答
0

试试下面的代码:

$totalprices = array();
$qualityscores = array();
foreach ($array as $value) {
    $totalprices[] = $value['totalprice'];
    $qualityscores[] = $value['qualityscore'];
}

array_multisort($totalprices, SORT_ASC, $qualityscores, SORT_DESC, $array);

RTM http://php.net/manual/en/function.array-multisort.php :)

于 2013-01-09T12:45:10.050 回答