如何在php中按2个值排序多维数组

时间:2013-01-09 12:29:58

标签: php multidimensional-array sorting

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

不幸的是,这不起作用。有没有人对这个功能有点精明,可能知道我哪里出错了?或者如果有这个功能的简单替代品?

提前致谢!

2 个答案:

答案 0 :(得分:3)

使用usort()功能。

你显然需要为它编写自己的排序功能。将该函数传递到usort()以按您需要的任何条件排序。

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

答案 1 :(得分: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:)