0

I have an array that consists of names of people and their height. I need to find the average height using the function array_reduce which is wrapped in a function assigned to a variable. Below is the code I wrote so far. Everything seems to be working until I pass the array to the array_reduce function which is then NULLed. I tried to explain it with comments in the code. Why is the array suddenly deleted and how do I pass it to the most inner function?

$people = [
  ['name'=> 'John'  , 'height'=> 1.65],
  ['name'=> 'Peter' , 'height'=> 1.85],
  ['name'=> 'Silvia', 'height'=> 1.69],
  ['name'=> 'Martin', 'height'=> 1.82]
];


$filter = function  ($people)
{
    $averageHeight = 0;
    $count = count($people);
    echo $count;
    //if I var_dump($people) - the array $people is FINE.
    return array_reduce(
            $people,
            function ($people) use (&$averageHeight, &$count)
            {
                $averageHeight += $people['height'] / $count;    
                return $averageHeight;
                //var_dump($people); - var people is NULL. Where did it go?
            }
    );
};

print_r($filter($people));
2
  • The reason $people is null is because it's the first function parameter you named people and not the actual array you declared above. PHP refers to this parameter as $carry which is the carried value. If you don't provide a default carry value it starts off as null. Commented Sep 27, 2017 at 13:36
  • And an alternative way of calculating the average height: $averageHeight = array_sum(array_column($people, 'height')) / count($people); Commented Sep 27, 2017 at 13:42

2 Answers 2

3

This is how you can use array_reduce to get an average:

function array_average($array) {
    $carry = null;
    $count = count($array);
    return array_reduce($array, function ($carried, $value) use ($count) {
         return ($carried===null?0:$carried) + $value/$count;
    },$carry);
}

The function will take the accumulator and the value as parameters and should return the value to be carried to the next element.

You can use it as:

 $averageheight = array_average(array_column($people,"height"));

This results in:

1.7525

Here's an example:

http://sandbox.onlinephpfunctions.com/code/a0bd15fb66061340ceca9e743418575d7990b6c3

If you need this whole thing as a callable then do:

function array_average($array) {
    $carry = null;
    $count = count($array);
    return array_reduce($array, function ($carried, $value) use ($count) {
         return ($carried===null?0:$carried) + $value/$count;
    },$carry);
}

$filter = function ($people) {
     return array_average(array_column($people,"height"));
};

 print_r($filter($people));

http://sandbox.onlinephpfunctions.com/code/dd2ec178be3989f8d9dd9d9ef5d270c7d40b8be7

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, this works and is great but I need to assign the function to a variable. I tried like this print_r($filter(array_average(array_column($people,"height")))); but I get Parse error: syntax error, unexpected 'print_r' (T_STRING)
@pidari updated. You can also declare the function inside the variable but it's quite a generic one so you may find uses for it elsewhere as well.
0
Use this code, where the total array count we didn't find out inside the sum function so, i will use this into the print_r() function.


<?php

function sum($carry, $p)
{
    $carry += $p['height'];
    return $carry;
}

$people = [
  ['name'=> 'John'  , 'height'=> 1.65],
  ['name'=> 'Peter' , 'height'=> 1.85],
  ['name'=> 'Silvia', 'height'=> 1.69],
  ['name'=> 'Martin', 'height'=> 1.82]
];

$k = 0;
$k = array_reduce($people ,"sum");
print_r($k/count($people));
?>

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.