1

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!

1
  • foreach loop, use the keys Commented Oct 2, 2014 at 22:25

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

yeah, a had a string in mind, because in the end what I really need is a string just like [401,170,118],[356,181,120],... This goes to google charts :-D Do you have any idea?
Many thanks @Dagon! Both solutions are working just fine! My bad here ;-)
Hey @Dagon we forgot to include the $key in the string. The chart will only work if we insert the year as well. Sorry that I didn't mentioned it before. We'll need something like [2012,401,170,118],[2013, 356,181,120]. Could you please still help me?
I tried: foreach($resultMax as $k=>$v){ $new.='['.$v[$k].','.$v.','.$result06[$k].','.$resultMin[$k].'],'; } but then received a Uninitialized string offset: error.
it would be $new.='['.$k.','.$v.','.$average[$k].','.$max[$k].'],';
0

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]
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.