我如何排序一个多维数组在php 2值


How do I sort a multidimensional array in php by 2 values

我一直在玩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);

不幸的是,这不起作用。有没有人对这个功能更有悟性,可能知道我错在哪里?或者是否有一个简单的替代函数?

提前感谢!

使用usort()函数

显然需要为它编写自己的排序函数。将该函数传递给usort(),以根据需要的任何标准进行排序。

请参阅上面链接的手册页,了解如何定义排序函数。

试试下面的代码:

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