1

I have an array of temperature data by hour. Some hours have zero data instead of a temp. When graphing using Google Charts, the zero causes the line graph to plummet. My temporary fix was to replace the zero values with null, causing a break in the line graph. The ideal solution would be to take the values on either side of the zero, and average them. The array is in order by hour. Help?

$array = array(
    "1AM" => "65",
    "2AM" => "66",
    "3AM" => "68",
    "4AM" => "68",
    "5AM" => "68",
    "6AM" => "0",
    "7AM" => "70",
    "8AM" => "71",
    "9AM" => "71",
    "10AM" => "73",
);

Here's my script replacing the 0's with nulls:

  $array = array ();
  foreach($parsed_json->history->observations as $key => $value) {
    $temp = (int)$value->tempi;
    if ($temp==0) {
        str_replace(0, null, $temp);
    }
    $hour = $value->date->hour;
    $array[$hour] = $temp;
  };

This Example would work great if the data was mine, but alas, it's from a JSON feed.

Would I use an array_walk() sort of deal? How would I reference the current place in the array? Any help is appreciated!

1
  • You reference the current place in array with $key. Commented Aug 2, 2012 at 16:26

3 Answers 3

2

I would scratch out the null portion, and just foreach-loop through the final array.

So, change your current code to:

$array = array ();

foreach($parsed_json->history->observations as $key => $value) {
    $temp = (int)$value->tempi;
}

$hour = $value->date->hour;
$array[$hour] = $temp;

And add this below it:

foreach($array as $hour => $temp){

  if($temp == "0"){
      $numHour = $hour[0];
      $hourPlus  = ($numHour + 1) . "AM";
      $hourMinus = ($numHour - 1) . "AM";

      $valuePlus  = $array[$hourPlus];
      $valueMinus = $array[$hourMinus];

      $average = ($valuePlus + $valueMinus)/2;

      $array[$hour] = $average;
  }

}
?>

This of course assumes that the values on either side of the zero are also not zero. You may want to add a check for that somewhere in there.

Tested and proven method.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked! Thanks! Other days have PM's as well, but I think I have that figured out. Also put a check in for consecutive zeros, and if it's a zero at the end or start of the array. Thanks!
Sweet, glad it worked for you! Yeah, you can account for the AM or PM fairly easily.
0

Couldn't you do something along the lines of:

str_replace(0, ((($key-1)+($key+1))/2), $temp);

Where $key is array position, take the value before 0 and after 0 add them and divide them by 2 to get the average.

1 Comment

You could also use array_keys() to do the +1 -1 quickly
0

I'll let you sort out what happens if first, last or consecutive values are 0.

$the_keys=array_keys($array);
foreach($the_key as $index=>$key)
{
    if($array[$key]==0)
    {
        $array[$key]=($array[$the_key[$index-1]]+$array[$the_key[$index+1]]/2);
    }
}

1 Comment

That should be $value==0, also $the_key should be $the_keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.