I have a problem. Multiple, actually, but if you can possibly help me with this one, many of the others will improve as well :)
So I have a huge dataset, from which I have pulled an array of values as follows:
Array
(
[0] => Array
(
[itemid] => 11802
[measurement] => 43.052
[moddate] => 2010-12-22
)
[1] => Array
(
[itemid] => 11802
[measurement] => 55.801
[moddate] => 2011-01-15
)
[2] => Array
(
[itemid] => 11802
[measurement] => 64.281
[moddate] => 2011-02-05
)
[3] => Array
(
[itemid] => 10636
[measurement] => 28.432
[moddate] => 2010-11-20
)
)
To construct a graph, I have to loop through a series of dates from, say, 1 October to 1 March. Within that loop at each date I need to look to my data array above and see if there is a measurement/s on that date. If no, I just put a blank line into my graph string and loop to the next date to continue the check.
If YES, then I need to create a point on my graph. This is a moving average graph however, so I need to go look into the array again to collect all readings within three days either side of the current reading date and average them to create my datapoint.
Somewhere between reading the php manual on array_filter and array_search for the fiftieth time my brain exploded. I’m just not getting it.
I THINK it’s array filter that I want - twice I expect - and then if I get my filtered array of dates for the moving average I presume I can array_sum and array_count to produce an average.
It’s the callback function that I don’t get - actually I struggle with functions in general. All I want is for it to check if it can find the matching date - just match the date as a string to the current date in my loop - doesn’t have to do anything fancy. how wouldI write that function?
From the manual:
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
function even($var)
{
// returns whether the input integer is even
return(!($var & 1));
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>