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()必要な基準で並べ替えます。

sort 関数を定義する方法については、上記のリンクのマニュアル ページを参照してください。

于 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 に答える