homepage Welcome to WebmasterWorld Guest from 54.243.13.30
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Filtering an array and then working with filter results
(or searching?)
deejay




msg:4229828
 3:12 am on Nov 13, 2010 (gmt 0)

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"));
?>

 

deejay




msg:4229893
 8:55 am on Nov 13, 2010 (gmt 0)

progress, maybe.

If my array above is called $holdallavg and the current date in my loop is called $cdate, then I have the following sorted for array_filter:


function minefunction($v)
{
if ($v==$cdate)
{
return true;
}
return false;
}

print_r(array_filter($holdallavg,"minefunction"));


It's returning an empty array in all cases though.

I'm assuming it's becuase the array is multi-dimensional?

enigma1




msg:4229916
 11:10 am on Nov 13, 2010 (gmt 0)

You cannot compare the input with an undefined variable. Where is $cdate defined?

deejay




msg:4229917
 11:17 am on Nov 13, 2010 (gmt 0)

Hi Enigma

Sorry, should have been clearer - cdate is defined in the loop that this sits inside.

I have checked and cdate does exist and is being passed in the correct form (yyyy-mm-dd) to be compared to the date value in the array

(past midnight here, so will sign off after this post)

enigma1




msg:4229938
 1:26 pm on Nov 13, 2010 (gmt 0)

The input you're going to compare will be an array. Can you post the whole code of the callback function then? (minefunction) Because I do not see in it wherer the $cdate is declared.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved