0

I have two arrays like so (however there can be more or less than 2 (any amount)):

[0] => Array
    (
        [assessedUsers] => Array
            (
                [0] => Array
                    (
                        [scores] => Array
                            (
                                [0] => 10
                                [1] => 10
                                [2] => 10
                                [3] => 10
                            )

                    )

                [1] => Array
                    (
                        [scores] => Array
                            (
                                [0] => 9
                                [1] => 10
                                [2] => 0
                                [3] => 9
                            )

                    )
            )
    )

Where the length of the scores array is always the same in both arrays.

I would like to take each element from each array, one by one, and average them, then append them into a new array.

For example, the output of my desired function would look like this:

[1] => Array
    (
        [scores] => Array
            (
                [0] => 9.5
                [1] => 10
                [2] => 5
                [3] => 9.5
            )

    )

Is there a function that can do this, or do I need a couple nested for() loops? If I need to use forl loops how would I go about doing it? I'm a little confused on the logic behind it.

Currently what I have is:

for ($i = 0; $i < sizeof($data["assessedUsers"]); $i++) {
    for ($j = 0; $j < sizeof($data["assessedUsers"][$i]["scores"]); $j++) {

    }
}

and I'm a little confused as to what to where to go next. Thanks in advance!

4
  • 3
    You need some loops. Commented Oct 8, 2014 at 19:25
  • Do you have only two array or there might be more? Commented Oct 8, 2014 at 19:45
  • There could and will be more. Commented Oct 8, 2014 at 19:47
  • @php than you should state it in your question Commented Oct 8, 2014 at 19:47

3 Answers 3

1
$mean = array_map( function($a, $b) { return ($a + $b) / 2; },
          $data['assessedUsers'][0]['scores'],
          $data['assessedUsers'][1]['scores']
        );

var_dump($mean);

And append $mean anywhere you want. Or do you have more than 2 arrays? You did not state it in your question.

ps: for any number of subarrays

$arr = array(
   array('scores' => array(10,10,10,10)),
   array('scores' => array(9,10,0,9)),
   array('scores' => array(1,2,3,4))
);

// remove arrays from the key
$tmp = call_user_func_array( function() { return func_get_args(); }, 
  array_map( function($a) { return $a['scores']; }, $arr)
);

// add arrays by each element
$mean = array_map( function($val, $ind) use($tmp) {
    $sum = 0;
    foreach($tmp as $i => $t)
       $sum += $t[$ind];
    return $sum / ($i + 1); 
}, $tmp[0], array_keys($tmp[0]));

var_dump($mean);
Sign up to request clarification or add additional context in comments.

3 Comments

@php there is no 500.. only if you are using a very old php. what version do you have?
There also can be more than 2 arrays.
Well there is haha. I'm looking at the error right now. It's through Ajax.
0

Probably two loops:

$newarray();

foreach($main_array as $user) {
   foreach($user['assessedUser'][0]['scores'] as $score_key => $user0_value) {
           $user1_value = $user['assessedUser'][1]['scores'][$score_key];
           $average = ($user1_value + $user0_value) / 2;
           ... stuff into new array
   }
}

1 Comment

There will be more than two arrays. I don't know what the length is.
0

I have solution for you, hope this help :)

$scores = array();

for ($i = 0; $i < sizeof($data["assessedUsers"]); $i++) {       
    for ($j = 0; $j < sizeof($data["assessedUsers"][$i]["scores"]); $j++) {
        if(isset($scores[$j])){
            $scores[$j] = ($scores[$j] + $data["assessedUsers"][$i]["scores"][$j]) / ($i +1);
        }else{
            $scores[] = $data["assessedUsers"][$i]["scores"][$j];
        }
    }
}

$scores[] = $scores;

view Example :) http://codepad.org/upPjMEym

12 Comments

500 internal server error. I don't think some of that is proper PHP syntax.
check Array(); or array() check update answer
Pretty sure it's not only that it should be $scores = array(); but it shouldn't be $scores[], because I'm still getting 500 internal server error.
I don't have any IDE for php, so i thing error in $scores = array(); replace Array() to array()
this / $i +1 change to /($i +1) this error when $i = 0;
|

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.