試試這個https://eval.in/829000
<?php
$array1 = Array
(
0 => Array ('Month' => 'Jan 2015', 'Total' => 10),
1 => Array ('Month' => 'Feb 2015', 'Total' => 8),
2 => Array ('Month' => 'Mar 2015', 'Total' => 10)
);
$array2 = Array
(
0 => Array('Month' => 'Jan 2016', 'Total' => 7),
1 => Array('Month' => 'Feb 2016', 'Total' => 5),
2 => Array('Month' => 'Mar 2016', 'Total' => 15)
);
$array3 = Array
(
0 => Array('Month' => 'Jan 2017', 'Total' => 13),
1 => Array('Month' => 'Feb 2017', 'Total' => 10),
2 => Array('Month' => 'Mar 2017', 'Total' => 11)
);
//ordered months names in lower case
$monthsNames = ['jan','feb','mar','abr','may','jun','jul','aug','sept','oct','nov','dec'];
$months = [];
$totals = [];
$orderedMonths = [];
$orderedTotals = [];
$orderedIndexes = [];
foreach($array1 as $array){
$months[] = $array['Month'];
$totals[] = $array['Total'];
}
foreach($array2 as $array){
$months[] = $array['Month'];
$totals[] = $array['Total'];
}
foreach($array3 as $array){
$months[] = $array['Month'];
$totals[] = $array['Total'];
}
//now the $months and $totals has all the values but not ordered, we need to order them
//get the indexes order;
foreach ($monthsNames as $monthName){
$index = 0; //reset the index for the next month
foreach ($months as $inputMonth){
//if the $inputMonth == $monthName then collected it's index
if (strpos(strtolower($inputMonth), $monthName) !== false){
$orderedIndexes[] = $index;
}
$index ++;
}
}
//build the final ordered array
foreach ($orderedIndexes as $index){
$orderedMonths[] = $months[$index];
$orderedTotals[] = $totals[$index];
}
var_dump($orderedMonths);
var_dump($orderedTotals);
exit;
?>
這個輸出
array(9) {
[0]=>
string(8) "Jan 2015"
[1]=>
string(8) "Jan 2016"
[2]=>
string(8) "Jan 2017"
[3]=>
string(8) "Feb 2015"
[4]=>
string(8) "Feb 2016"
[5]=>
string(8) "Feb 2017"
[6]=>
string(8) "Mar 2015"
[7]=>
string(8) "Mar 2016"
[8]=>
string(8) "Mar 2017"
}
array(9) {
[0]=>
int(10)
[1]=>
int(7)
[2]=>
int(13)
[3]=>
int(8)
[4]=>
int(5)
[5]=>
int(10)
[6]=>
int(10)
[7]=>
int(15)
[8]=>
int(11)
}
注意,在你想要的結果的總數是不是你想要的正確順序,因爲2015年2月8不是5
也注意到,如果你相信幾個月字符串格式,您還可以使用PHP日期和時間函數。
什麼問題? – AbraCadaver
我需要提示如何處理它,然後我會編寫代碼。 –