我有一个看起来像这样的2d PHP数组。
Array
(
[0] => Array
(
[year] => 1990
[Hydro] => 17071.0288392
[Coal] =>
)
[1] => Array
(
[year] => 2000
[Hydro] => 6708.00288392
[Coal] =>
)
[2] => Array
(
[year] => 1990
[Hydro] =>
[Coal] => 17071.0288392
)
[3] => Array
(
[year] => 2000
[Hydro] =>
[Coal] => 2100.0288392
)
)
有没有办法根据年份键的值合并元素,所以我会得到这样的东西 预期结果
Array
(
[0] => Array
(
[year] => 1990
[Hydro] => 17071.0288392
[Coal] => 17071.0288392
)
[1] => Array
(
[year] => 2000
[Hydro] => 6708.00288392
[Coal] => 2100.0288392
)
)
非常感谢你!
答案 0 :(得分:1)
<?php
$arrNew = array();
foreach ($arrCurr as $arr) {
$arrTemp = array();
for ($i=1; $i < count($arr); $i++) {
$arrTemp[] = $arr[$i];
}
$arrNew[$arr['year']][] = $arrTemp;
}
print_r($arrNew);
?>