形式从关联数组
新的阵列我有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];
回答:
试试这个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日期和时间函数。
回答:
您可以使用foreach循环:
$months = array(); $totals = array();
foreach($array as $var){
$months[] = $var['Month'];
$totals[] = $var['Total'];
}
以上是 形式从关联数组 的全部内容, 来源链接: utcz.com/qa/259149.html