tommyvallier
tommyvallier

Reputation: 67

PHP: Average of multi-dimensional array

I'm trying to build a tool to average values from a multidimensional array in PHP. Array sample:

$t[1][1]=2;
$t[1][2]=3;
$t[2][1]=5;
$t[3]=6;
$t[4][1][1]=9;
$t[4][1][2]=10;
$t[4][2][1]=12;
$t[4][2][2]=13;

Notice, too, that parents have no value (Since they have children). I have this:

function chklevel($s) {
  $l = explode(".",$s);
}

Which gives me the ability to call chklevel as

chklevel("4.2.2")

and have it return 13, but I also want to be able to call

chklevel("4")

and have it return 11 (Which is the average of 4.1.1(9), 4.1.2(10), 4.2.1(12) and 4.2.2(13).

Any thoughts?

Upvotes: 2

Views: 1134

Answers (2)

Czechnology
Czechnology

Reputation: 14992

Here's my solution:

function chklevel($t) {

  for ($i = 1; $i < func_num_args(); $i++) {
    if (is_array($t) && isset($t[func_get_arg($i)]))
      $t = $t[func_get_arg($i)];
  }

  return GetAvg($t);
}

function GetAvg($arr) {

  if (is_array($arr)) {
    $avg = 0;
    foreach ($arr as $v) {
      $avg += GetAvg($v);
    }
    $avg /= count($arr);

  } else if (is_numeric($arr)) {
    $avg = $arr;
  }

  return $avg;
}

I prefer function call like this: chklevel($t, 4,2,2); but you could easily modify it for strings:

function chklevel($t, $s) {
  $indexes = explode(".", $s);
  for ($i = 0; $i < count($indexes); $i++) {
    if (is_array($t) && isset($t[$indexes[$i]]))
      $t = $t[$indexes[$i]];
  }

  return GetAvg($t);
}

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101604

I had to do it in two functions (just because of the recursive nature of the search, but here's my bid:

function mdarray_avg($array, $level = ''){
  if (!is_array($array)) throw new Exception("First argument must be an array");
  if (!is_string($level)) throw new Exception("Second argument must be string");

  $ary = $array;
  $levels = explode('.',$level);
  foreach ($levels as $lvl){
    if (array_key_exists($lvl,$ary))
      $ary = $ary[$lvl];
    else
      throw new Exception("Level {$level} doesn't exist");
  }

  $numbers = _mdarray_avg($ary);
  $sum = array_sum($numbers);
  return ($sum > 0 ? $sum / count($numbers) : 0);
}

function _mdarray_avg($array){
  if (!is_array($array)) return array($array);
  $numbers = array();
  foreach ($array as $element){
    if (is_array($element))
      $numbers = array_merge($numbers,_mdarray_avg($element));
    else
      $numbers[] = $element;
  }
  return $numbers;
}

Use it like so:

echo "Average: ".mdarray_avg($t,'4'); // returns 11, as expected. 

Where $t is the array to search through, and the '4' is the level you're searching (could also be '1', '4.2', etc.

Also, a fun note, exempting the second parameter averages the whole array, in this case I returned 7.5 (avg(2,3,5,6,9,10,12,13))

Upvotes: 1

Related Questions