2017-07-06 96 views
0

新的陣列我有3個數組,看起來像這樣形式從關聯數組

Array1 
(
[0] => Array 
    (
     [Month] => 'Jan 2015' 
     [Total] => 10 
    ) 

[1] => Array 
    (
     [Month] => 'Feb 2015' 
     [Total] => 8 
    ) 

[2] => Array 
    (
     [Month] => 'Mar 2015' 
     [Total] => 10 
    ) 
) 

Array2 
(
[0] => Array 
    (
     [Month] => 'Jan 2016' 
     [Total] => 7 
    ) 

[1] => Array 
    (
     [Month] => 'Feb 2016' 
     [Total] => 5 
    ) 

[2] => Array 
    (
     [Month] => 'Mar 2016' 
     [Total] => 15 
    ) 
) 

Array3 
(
[0] => Array 
    (
     [Month] => 'Jan 2017' 
     [Total] => 13 
    ) 

[1] => Array 
    (
     [Month] => 'Feb 2017' 
     [Total] => 10 
    ) 

[2] => Array 
    (
     [Month] => 'Mar 2017' 
     [Total] => 11 
    ) 
) 

所有3個數組大小相同的。

我想要實現的是以相同順序依次輸出相同的月份和相應的值。這些新型陣列應該是這樣的:

期望的結果:

$months = array['Jan 2015','Jan 2016','Jan 2017', 'Feb 2015', 'Feb 2016'...'Mar 2016','Mar 2017']; 
$totals = array[10,7,13,5,5,...,15,11]; 
+1

什麼問題? – AbraCadaver

+0

我需要提示如何處理它,然後我會編寫代碼。 –

回答

0

試試這個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日期和時間函數。

+0

使用此代碼實現了所需的結果。但是,如果你可以運行我周圍發生的事情'foreach($ monthsNames as $ monthName){ $ index = 0; (strpos(strtolower($ inputMonth),$ monthName)!== false){ $ orderIndexes [] = $ index; } $ index ++; } –

+0

@ScottMensah因爲我們已經知道我們的'$ monthsNames'是有序的,所以我們可以循環它的值並收集'jans'的索引,然後'febs'的索引,然後'行進的索引',.. ,, ...,..到十二月份的索引...之後,'$ orderedIndexes'包含月份排序的'$ months和$ totals'數組中的值的索引..我會也更新答案以添加評論到這一點 –

0

您可以使用foreach循環:

$months = array(); 
$totals = array(); 

foreach($array as $var){ 
    $months[] = $var['Month']; 
    $totals[] = $var['Total']; 
} 
+0

我得到的輸出看起來不錯。 'Array([0] => Jan 2015 [1] => Feb 2015 [2] => Mar 2015)'。問題是'$ months []'的第二個元素是'2016年1月'的第二個元素。 –