Jump to content
smith.james0

Array checking help

Recommended Posts

Hi,

 

I am sure that the solution is easy but it's flummoxed me! I have a python script that sometimes doesn't record the temperature so I end up with 23 readings instead of 24 per day, or less.

My php script reads the date from the db and puts it into a array to use in a graph. My problem is that I need to check the correct temperature reading go with the correct hour of the day.

Array
(
    [0] => Array
        (
            [0] => 00
            [1] => 4.30
        )

    [1] => Array
        (
            [0] => 01
            [1] => 4.30
        )

    [2] => Array
        (
            [0] => 02
            [1] => 4.10
        )

    [3] => Array
        (
            [0] => 03
            [1] => 4.00
        )

    [4] => Array
        (
            [0] => 05
            [1] => 3.70
        )
etc etc..

Above [0][0] is the time and [0][1] is the temperature, what I need is for the time to match the array key. So in [4][0] the time is 05 but the key is 04 i.e...

Array
(
    [0] => Array
        (
            [0] => 00
            [1] => 4.30
        )

    [1] => Array
        (
            [0] => 01
            [1] => 4.30
        )

    [2] => Array
        (
            [0] => 02
            [1] => 4.10
        )

    [3] => Array
        (
            [0] => 03
            [1] => 4.00
        )

    [4] => Array
        (
            [0] => 04
            [1] => 
        )

    [5] => Array
        (
            [0] => 05
            [1] => 3.70
        )

This is the code I use to make the array

while($row = $result->fetch_array())
{

$inside[] = $row;

$date = strtotime($inside[$i][Time]);
$inside[$i][4] = date('G', $date);
$inside[$i][2] = $inside[$i][0];

unset($inside[$i][Temp]);
unset($inside[$i][Time]);
unset($inside[$i][1]);
unset($inside[$i][0]);


++$i;

}

I can get it to check the time against the key, but I can not get it to correct the key to the time. 

Can anyone help??

 

Many thanks James

 

 

Share this post


Link to post
Share on other sites

Keep it simple

$inside = [];
while($row = $result->fetch_array())
{
    $hour = date('G', strtotime($row['time']));
    $inside[$hour] = $row['temp'];
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.