Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020004
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:10:28+00:00 2026-05-20T10:10:28+00:00

I’m trying to build a tool to average values from a multidimensional array in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:10:28+00:00Added an answer on May 20, 2026 at 10:10 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.