2017-02-10 7 views
2

次の配列をどのように並べ替えるのですか? 配列をサブ配列値で並べ替え

トップレベルの配列メータータイプ:

昔ながらメートル、それらの内部

で、 」「スマートメーターと」そこにも配列されている様々な関税がある。しかし、それぞれの関税を持っています。総計では、各メータータイプの配列内の料金を並べ替える必要があります。

配列を生成するたびに料金表の名前を知ることはできません。タリフ名873.03195-SDD-E1R-VAR-0002_SDD-G1R-VAR-0002は毎回異なる可能性があります。

array:2 [ 
"With a Smart meter" => array:3 [ 
    "873.03195-SDD-E1R-VAR-0002_SDD-G1R-VAR-0002" => array:4 [ 
    "electricity" => array:4 [ 
    "code" => "SDD-E1R-VAR-0002" 
    "name" => "Smart Go with the flow" 
    "costs" => array:4 [ 
     "year" => "464.13" 
     "month" => "38.68" 
     "week" => "8.93" 
     "savings" => 27.0 
    ] 
    "details" => array:4 [ 
     "name" => "Smart Go with the flow" 
     "unitRate" => array:3 [ 
     "standard" => 12.4992 
     "night" => null 
     "day" => null 
     ] 
     "standingCharge" => 21.0 
     "comparisonRate" => 14.97 
    ] 
    ] 
    "gas" => array:4 [ 
    "code" => "SDD-G1R-VAR-0002" 
    "name" => "Smart Go with the flow - Gas" 
    "costs" => array:4 [ 
     "year" => "408.91" 
     "month" => "34.08" 
     "week" => "7.86" 
     "savings" => 160.0 
    ] 
    "details" => array:4 [ 
     "name" => "Smart Go with the flow - Gas" 
     "unitRate" => 2.5998 
     "standingCharge" => 22.995 
     "comparisonRate" => 3.27 
    ] 
    ] 
    "display" => array:6 [ 
    "code" => "SDD-E1R-VAR-0002_SDD-G1R-VAR-0002" 
    "name" => "Smart Go with the flow" 
    "idPaymentMethod" => "4" 
    "textPaymentMethod" => "Smart Direct Debit" 
    "costs" => array:4 [ 
     "year" => "873.03" 
     "month" => "72.75" 
     "week" => "16.79" 
     "savings" => 186.0 
    ] 
    "podCopy" => array:2 [ 
     "subHeading" => "Go with the flow" 
     "listContent" => """ 
     <ul>\r\n 
     \t<li><span style="font-family: Arial, sans-serif; font-size: 14px;">Say goodbye to estimated billing with a TOTO smart meter</span></li>\r\n 
     \t<li><span style="font-family: Arial, sans-serif; font-size: 14px;">Keep track of your usage with our FREE Smart Meter and in-home display</span></li>\r\n 
     </ul> 
     """ 
    ] 
    ] 
    "total" => "873.03" 
] 
"1034.19225-SDD-E1R-VAR-0004_SDD-G1R-VAR-0004" => array:4 [ 
    "electricity" => array:4 [ 
    "code" => "SDD-E1R-VAR-0004" 
    "name" => "TOTO Smart Evergreen Saver" 
    "costs" => array:4 [ 
     "year" => "547.32" 
     "month" => "45.61" 
     "week" => "10.53" 
     "savings" => -57.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Smart Evergreen Saver" 
     "unitRate" => array:3 [ 
     "standard" => 15.183 
     "night" => null 
     "day" => null 
     ] 
     "standingCharge" => 21.0 
     "comparisonRate" => 17.66 
    ] 
    ] 
    "gas" => array:4 [ 
    "code" => "SDD-G1R-VAR-0004" 
    "name" => "TOTO Smart Evergreen Saver - Gas" 
    "costs" => array:4 [ 
     "year" => "486.87" 
     "month" => "40.57" 
     "week" => "9.36" 
     "savings" => 82.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Smart Evergreen Saver - Gas" 
     "unitRate" => 3.2235 
     "standingCharge" => 22.995 
     "comparisonRate" => 3.89 
    ] 
    ] 
    "display" => array:6 [ 
    "code" => "SDD-E1R-VAR-0004_SDD-G1R-VAR-0004" 
    "name" => "TOTO Smart Evergreen Saver" 
    "idPaymentMethod" => "4" 
    "textPaymentMethod" => "Smart Direct Debit" 
    "costs" => array:4 [ 
     "year" => "1,034.19" 
     "month" => "86.18" 
     "week" => "19.89" 
     "savings" => 25.0 
    ] 
    "podCopy" => array:2 [ 
     "subHeading" => "Evergreen Saver" 
     "listContent" => """ 
     <ul>\r\n 
     \t<li>No contract, no end date, no hassle.</li>\r\n 
     \t<li>Say goodbye to estimated billing with a TOTO Smart meter and FREE in home display.</li>\r\n 
     \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n 
     \t<li>Prompt friendly service from the friendly TOTO team.<br />\r\n 
     \t</li>\r\n 
     </ul> 
     """ 
    ] 
    ] 
    "total" => "1,034.19" 
] 
"894.71025-SDD-E1R-VAR-0003_SDD-G1R-VAR-0003" => array:4 [ 
    "electricity" => array:4 [ 
    "code" => "SDD-E1R-VAR-0003" 
    "name" => "TOTO Smart Discount Variable Saver" 
    "costs" => array:4 [ 
     "year" => "485.80" 
     "month" => "40.48" 
     "week" => "9.34" 
     "savings" => 5.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Smart Discount Variable Saver" 
     "unitRate" => array:3 [ 
     "standard" => 13.1985 
     "night" => null 
     "day" => null 
     ] 
     "standingCharge" => 21.0 
     "comparisonRate" => 15.67 
    ] 
    ] 
    "gas" => array:4 [ 
    "code" => "SDD-G1R-VAR-0003" 
    "name" => "TOTO Smart Discount Variable Saver - Gas" 
    "costs" => array:4 [ 
     "year" => "408.91" 
     "month" => "34.08" 
     "week" => "7.86" 
     "savings" => 160.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Smart Discount Variable Saver - Gas" 
     "unitRate" => 2.5998 
     "standingCharge" => 22.995 
     "comparisonRate" => 3.27 
    ] 
    ] 
    "display" => array:6 [ 
    "code" => "SDD-E1R-VAR-0003_SDD-G1R-VAR-0003" 
    "name" => "TOTO Smart Discount Variable Saver" 
    "idPaymentMethod" => "4" 
    "textPaymentMethod" => "Smart Direct Debit" 
    "costs" => array:4 [ 
     "year" => "894.71" 
     "month" => "74.56" 
     "week" => "17.21" 
     "savings" => 165.0 
    ] 
    "podCopy" => array:2 [ 
     "subHeading" => "Discount Variable Saver" 
     "listContent" => """ 
     <ul>\r\n 
     \t<li>Discounted rate for 12 months.</li>\r\n 
     \t<li>Say goodbye to estimated billing with a TOTO Smart meter and FREE in home display.</li>\r\n 
     \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n 
     \t<li>12 month contract.</li>\r\n 
     </ul> 
     """ 
    ] 
    ] 
    "total" => "894.71" 
] 
] 
"With an old-fashioned meter" => array:3 [ 
"873.03195-LDD-E1R-VAR-0002_LDD-G1R-VAR-0002" => array:4 [ 
    "electricity" => array:4 [ 
    "code" => "LDD-E1R-VAR-0002" 
    "name" => "Go with the flow 2" 
    "costs" => array:4 [ 
     "year" => "464.13" 
     "month" => "38.68" 
     "week" => "8.93" 
     "savings" => 27.0 
    ] 
    "details" => array:4 [ 
     "name" => "Go with the flow 2" 
     "unitRate" => array:3 [ 
     "standard" => 12.4992 
     "night" => null 
     "day" => null 
     ] 
     "standingCharge" => 21.0 
     "comparisonRate" => 14.97 
    ] 
    ] 
    "gas" => array:4 [ 
    "code" => "LDD-G1R-VAR-0002" 
    "name" => "Go with the flow 2 - Gas" 
    "costs" => array:4 [ 
     "year" => "408.91" 
     "month" => "34.08" 
     "week" => "7.86" 
     "savings" => 160.0 
    ] 
    "details" => array:4 [ 
     "name" => "Go with the flow 2 - Gas" 
     "unitRate" => 2.5998 
     "standingCharge" => 22.995 
     "comparisonRate" => 3.27 
    ] 
    ] 
    "display" => array:6 [ 
    "code" => "LDD-E1R-VAR-0002_LDD-G1R-VAR-0002" 
    "name" => "Go with the flow 2" 
    "idPaymentMethod" => "4" 
    "textPaymentMethod" => "Direct Debit" 
    "costs" => array:4 [ 
     "year" => "873.03" 
     "month" => "72.75" 
     "week" => "16.79" 
     "savings" => 186.0 
    ] 
    "podCopy" => array:2 [ 
     "subHeading" => "Go with the flow" 
     "listContent" => """ 
     <ul>\r\n 
     \t<li>No contract, no end-date, no hassle.</li>\r\n 
     \t<li>Your tariff can go up and down, we&rsquo;ll always tell you first.</li>\r\n 
     \t<li>Prompt personal service from the friendly TOTO team.</li>\r\n 
     </ul> 
     """ 
    ] 
    ] 
    "total" => "873.03" 
] 
"894.71025-LDD-E1R-VAR-0003_LDD-G1R-VAR-0003" => array:4 [ 
    "electricity" => array:4 [ 
    "code" => "LDD-E1R-VAR-0003" 
    "name" => "TOTO Discount Variable Saver" 
    "costs" => array:4 [ 
     "year" => "485.80" 
     "month" => "40.48" 
     "week" => "9.34" 
     "savings" => 5.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Discount Variable Saver" 
     "unitRate" => array:3 [ 
     "standard" => 13.1985 
     "night" => null 
     "day" => null 
     ] 
     "standingCharge" => 21.0 
     "comparisonRate" => 15.67 
    ] 
    ] 
    "gas" => array:4 [ 
    "code" => "LDD-G1R-VAR-0003" 
    "name" => "TOTO Discount Variable Saver - Gas" 
    "costs" => array:4 [ 
     "year" => "408.91" 
     "month" => "34.08" 
     "week" => "7.86" 
     "savings" => 160.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Discount Variable Saver - Gas" 
     "unitRate" => 2.5998 
     "standingCharge" => 22.995 
     "comparisonRate" => 3.27 
    ] 
    ] 
    "display" => array:6 [ 
    "code" => "LDD-E1R-VAR-0003_LDD-G1R-VAR-0003" 
    "name" => "TOTO Discount Variable Saver" 
    "idPaymentMethod" => "4" 
    "textPaymentMethod" => "Direct Debit" 
    "costs" => array:4 [ 
     "year" => "894.71" 
     "month" => "74.56" 
     "week" => "17.21" 
     "savings" => 165.0 
    ] 
    "podCopy" => array:2 [ 
     "subHeading" => "Discount Variable Saver" 
     "listContent" => """ 
     <ul>\r\n 
     \t<li>Discounted rate for 12 months.</li>\r\n 
     \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n 
     \t<li>12 month contract.</li>\r\n 
     \t<li>Prompt personal service from the friendly TOTO team.</li>\r\n 
     </ul> 
     """ 
    ] 
    ] 
    "total" => "894.71" 
] 
"1034.19225-LDD-E1R-VAR-0004_LDD-G1R-VAR-0004" => array:4 [ 
    "electricity" => array:4 [ 
    "code" => "LDD-E1R-VAR-0004" 
    "name" => "TOTO Evergreen Saver" 
    "costs" => array:4 [ 
     "year" => "547.32" 
     "month" => "45.61" 
     "week" => "10.53" 
     "savings" => -57.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Evergreen Saver" 
     "unitRate" => array:3 [ 
     "standard" => 15.183 
     "night" => null 
     "day" => null 
     ] 
     "standingCharge" => 21.0 
     "comparisonRate" => 17.66 
    ] 
    ] 
    "gas" => array:4 [ 
    "code" => "LDD-G1R-VAR-0004" 
    "name" => "TOTO Evergreen Saver - Gas" 
    "costs" => array:4 [ 
     "year" => "486.87" 
     "month" => "40.57" 
     "week" => "9.36" 
     "savings" => 82.0 
    ] 
    "details" => array:4 [ 
     "name" => "TOTO Evergreen Saver - Gas" 
     "unitRate" => 3.2235 
     "standingCharge" => 22.995 
     "comparisonRate" => 3.89 
    ] 
    ] 
    "display" => array:6 [ 
    "code" => "LDD-E1R-VAR-0004_LDD-G1R-VAR-0004" 
    "name" => "TOTO Evergreen Saver" 
    "idPaymentMethod" => "4" 
    "textPaymentMethod" => "Direct Debit" 
    "costs" => array:4 [ 
     "year" => "1,034.19" 
     "month" => "86.18" 
     "week" => "19.89" 
     "savings" => 25.0 
    ] 
    "podCopy" => array:2 [ 
     "subHeading" => "Evergreen Saver" 
     "listContent" => """ 
     <ul>\r\n 
     \t<li>No contract, no end date, no hassle.</li>\r\n 
     \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n 
     \t<li>Prompt friendly service from the friendly TOTO team.</li>\r\n 
     </ul> 
     """ 
    ] 
    ] 
    "total" => "1,034.19" 
] 

] ]

+0

あなた自身のソート関数を定義することになる 'usort'について読んでください。 http://php.net/manual/en/function.usort.php – num8er

+0

私はusortを使う必要があることを知っています。そのソート関数は私が苦労しています。 – user794846

+0

このように正確な属性を説明してください。あなたの質問では、どちらをソートするために使用したいのかわかりません – num8er

答えて

1

私はあなたがこののいくつかの種類をしたいと思います。

何か問題が生じた場合はコメントを入力します(私は自分のコードをチェックしていません)。

$unsortedData = [...]; 

$sortByTotal = function($a, b) { 
    $aTotal = (float)str_replace(',', '', $a['total']); // Your total value is string like "1,034.19" so making it normalized to easily compare 
    $bTotal = (float)str_replace(',', '', $b['total']); 

    return $aTotal <=> $bTotal; // read about "starship operator" in PHP 7+ 
}; 

foreach($unsortedData AS $meter => $records) { 
    usort($unsortedData[$meter], $sortByTotal); 
} 
+0

PHP 7+を使用している場合は、 '$ sortByTotal'の' return $ aTotal <=> $ bTotal; ' – bishop

+0

@bishopの宇宙船オペレータ - 完了(: – num8er