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?
I had to do it in two functions (just because of the recursive nature of the search, but here’s my bid:
Use it like so:
Where
$tis 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))