从关联数组中形成新数组

时间:2017-07-06 21:44:48

标签: php arrays associative-array

我有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];

2 个答案:

答案 0 :(得分:0)

您可以使用foreach循环:

"<>"

答案 1 :(得分: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日期和时间函数。