Reputation: 902
I have following arrays:
Min values:
[2006] => 117
[2007] => 117
[2008] => 117
[2009] => 117
[2010] => 117
[2011] => 117
[2012] => 118
[2013] => 132
Average:
[2006] => 170
[2007] => 174
...
[2013] => 166
Max values:
[2006] => 291
[2007] => 630
...
[2013] => 246
How can I merge them so I have following array with subarray:
[2006] => array (117, 170, 291),
[2007] => array (117, 174, 630),
...
[2013] => array (132, 166, 246)
A solution without subarrays would be apreciate as well. Something like that would solve the problem too:
[2006] => [117, 170, 291]
[2007] => [117, 174, 630]
...
[2013] => [132, 166, 246]
Many thanks in advance!
Upvotes: 1
Views: 77
Reputation: 94662
Its quite simple, just write a foreach loop, to loop over one of the arrays, I used the $mins array here.
Then grab the equivalent data from each of the other arrays and add them all into your new array using the key as the key to the new array.
$mins = array(2006 => 117, 2007 => 117, 2008 => 117,
2009 => 117, 2010 => 117, 2011 => 117,
2012 => 118, 2013 => 132);
$avgs = array(2006 => 170, 2007 => 174, 2008 => 169,
2009 => 179, 2010 => 180, 2011 => 181,
2012 => 182, 2013 => 183);
$maxs = array(2006 => 217, 2007 => 233, 2008 => 322,
2009 => 215, 2010 => 216, 2011 => 217,
2012 => 231, 2013 => 232);
$newArray = array();
foreach( $mins as $year => $min ) {
$newArray[$year] = array( $min, $avgs[$year], $maxs[$year] );
}
print_r( $newArray );
Output is :-
Array
(
[2006] => Array
(
[0] => 117
[1] => 170
[2] => 217
)
[2007] => Array
(
[0] => 117
[1] => 174
[2] => 233
)
[2008] => Array
(
[0] => 117
[1] => 169
[2] => 322
)
[2009] => Array
(
[0] => 117
[1] => 179
[2] => 215
)
[2010] => Array
(
[0] => 117
[1] => 180
[2] => 216
)
[2011] => Array
(
[0] => 117
[1] => 181
[2] => 217
)
[2012] => Array
(
[0] => 118
[1] => 182
[2] => 231
)
[2013] => Array
(
[0] => 132
[1] => 183
[2] => 232
)
)
And using the new array syntax it would be
$mins = [2006 => 117, 2007 => 117, 2008 => 117,
2009 => 117, 2010 => 117, 2011 => 117,
2012 => 118, 2013 => 132];
$avgs = [2006 => 170, 2007 => 174, 2008 => 169,
2009 => 179, 2010 => 180, 2011 => 181,
2012 => 182, 2013 => 183];
$maxs = [2006 => 217, 2007 => 233, 2008 => 322,
2009 => 215, 2010 => 216, 2011 => 217,
2012 => 231, 2013 => 232];
$newArray = [];
foreach( $mins as $year => $min ) {
$newArray[$year] = [ $min, $avgs[$year], $maxs[$year] ];
}
print_r( $newArray );
And if you want the result as an array of string just change it like this
foreach( $mins as $year => $min ) {
$newArray[$year] = sprintf('[%d,%d,%d]', $min, $avgs[$year], $maxs[$year]);
}
print_r( $newArray );
Giving this result
Array
(
[2006] => [117,170,217]
[2007] => [117,174,233]
[2008] => [117,169,322]
[2009] => [117,179,215]
[2010] => [117,180,216]
[2011] => [117,181,217]
[2012] => [118,182,231]
[2013] => [132,183,232]
)
Upvotes: 0
Reputation:
made assumption about array names:
$new=array();
foreach($min as $k=>$v){
$new[$k]=array($v,$average[$k],$max[$k]);
}
did not really understand what "solution without subarrays" meant - did you want a string or?
EDIT string creation
$new='';
foreach($min as $k=>$v){
$new.='['.$v.','.$average[$k].','.$max[$k].'],';
}
$new=rtrim($new,',');//remove last comma
Upvotes: 1