user14735672
user14735672

Reputation:

How can I reorganize an array?

I have the array:

$csv=[
["DATE"=>'2015-01-01T00:00:00',"TMP"=>'+2'],
["DATE"=>'2015-01-01T01:00:00',"TMP"=>'+1'],
["DATE"=>'2015-02-01T15:00:00',"TMP"=>'+1']]

I want to create an array:

$datearr=[
'2015-01-01'=>['00:00:00'=>'+2', '01:00:00'=>'+1', '02:00:00'=>'nodata', '03:00:00'=>'nodata', '04:00:00'=>'nodata', '05:00:00'=>'nodata', '06:00:00'=>'nodata', '07:00:00'=>'nodata', '08:00:00'=>'nodata', '09:00:00'=>'nodata', '10:00:00'=>'nodata', '11:00:00'=>'nodata', '12:00:00'=>'nodata', '13:00:00'=>'nodata', '14:00:00'=>'nodata', '15:00:00'=>'nodata', '16:00:00'=>'nodata', '17:00:00'=>'nodata', '18:00:00'=>'nodata', '19:00:00'=>'nodata', '20:00:00'=>'nodata', '21:00:00'=>'nodata', '22:00:00'=>'nodata', '23:00:00'=>'nodata' ],
'2015-02-01'=>['00:00:00'=>'nodata', '01:00:00'=>'nodata', '02:00:00'=>'nodata', '03:00:00'=>'nodata', '04:00:00'=>'nodata', '05:00:00'=>'nodata', '06:00:00'=>'nodata', '07:00:00'=>'nodata', '08:00:00'=>'nodata', '09:00:00'=>'nodata', '10:00:00'=>'nodata', '11:00:00'=>'nodata', '12:00:00'=>'nodata', '13:00:00'=>'nodata', '14:00:00'=>'nodata', '15:00:00'=>'+1', '16:00:00'=>'nodata', '17:00:00'=>'nodata', '18:00:00'=>'nodata', '19:00:00'=>'nodata', '20:00:00'=>'nodata', '21:00:00'=>'nodata', '22:00:00'=>'nodata', '23:00:00'=>'nodata' ]
   
]

And I have some problems in my code:

$datearr=[];

    $curdate='0';//currentdate. It will be '2015-01-01' after first iteration.
    $curtime='00:00:00';//current time

   $alltime=['00:00:00'=>'nodata', '01:00:00'=>'nodata', '02:00:00'=>'nodata', '03:00:00'=>'nodata', '04:00:00'=>'nodata', '05:00:00'=>'nodata', '06:00:00'=>'nodata', '07:00:00'=>'nodata', '08:00:00'=>'nodata', '09:00:00'=>'nodata', '10:00:00'=>'nodata', '11:00:00'=>'nodata', '12:00:00'=>'nodata', '13:00:00'=>'nodata', '14:00:00'=>'nodata', '15:00:00'=>'nodata', '16:00:00'=>'nodata', '17:00:00'=>'nodata', '18:00:00'=>'nodata', '19:00:00'=>'nodata', '20:00:00'=>'nodata', '21:00:00'=>'nodata', '22:00:00'=>'nodata', '23:00:00'=>'nodata'];
    foreach ($csv as $key=>$value){


        foreach($value as $k=>$val){

            if($k=="DATE"){
                $date=explode('T', $val);
                $curtime=$date[1];
                if($curdate==$date[0]) continue;
                else{


$datearr[$curdate]=$alltime;
                }
            }
if($k=="TMP"){
                foreach($datearr[$curdate] as $hour=>$tmp){
                    if ($hour==$curtime){
                        $tmp=$k;
                   }

            }

           }

Because it response only one:

array(1) { ["0"]=> array(24) { ["00:00:00"]=> string(6) "nodata" ["01:00:00"]=> string(6) "nodata" ["02:00:00"]=> string(6) "nodata" ["03:00:00"]=> string(6) "nodata" ["04:00:00"]=> string(6) "nodata" ["05:00:00"]=> string(6) "nodata" ["06:00:00"]=> string(6) "nodata" ["07:00:00"]=> string(6) "nodata" ["08:00:00"]=> string(6) "nodata" ["09:00:00"]=> string(6) "nodata" ["10:00:00"]=> string(6) "nodata" ["11:00:00"]=> string(6) "nodata" ["12:00:00"]=> string(6) "nodata" ["13:00:00"]=> string(6) "nodata" ["14:00:00"]=> string(6) "nodata" ["15:00:00"]=> string(6) "nodata" ["16:00:00"]=> string(6) "nodata" ["17:00:00"]=> string(6) "nodata" ["18:00:00"]=> string(6) "nodata" ["19:00:00"]=> string(6) "nodata" ["20:00:00"]=> string(6) "nodata" ["21:00:00"]=> string(6) "nodata" ["22:00:00"]=> string(6) "nodata" ["23:00:00"]=> string(6) "nodata" } }

Could you help me pls and show where i have mistakes, thnx

Upvotes: 0

Views: 48

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

A simple approach is to just split the date part into the date and the time(using T as the divider). Then create an output array with the date as the index and the time as a sub-index. In this code I've done the simple version of creating a template array with all of the time slots, there are bound to be some simpler way of doing this, but this will still work...

$csv=[
    ["DATE"=>'2015-01-01T00:00:00',"TMP"=>'+2'],
    ["DATE"=>'2015-01-01T01:00:00',"TMP"=>'+1'],
    ["DATE"=>'2015-02-01T15:00:00',"TMP"=>'+1']
];

$output = [];
$dayTemplate = ['00:00:00'=>'nodata', '01:00:00'=>'nodata', '02:00:00'=>'nodata',
        '03:00:00'=>'nodata', '04:00:00'=>'nodata', '05:00:00'=>'nodata',
        '06:00:00'=>'nodata', '07:00:00'=>'nodata', '08:00:00'=>'nodata',
        '09:00:00'=>'nodata', '10:00:00'=>'nodata', '11:00:00'=>'nodata',
        '12:00:00'=>'nodata', '13:00:00'=>'nodata', '14:00:00'=>'nodata',
        '15:00:00'=>'nodata', '16:00:00'=>'nodata', '17:00:00'=>'nodata',
        '18:00:00'=>'nodata', '19:00:00'=>'nodata', '20:00:00'=>'nodata',
        '21:00:00'=>'nodata', '22:00:00'=>'nodata', '23:00:00'=>'nodata' ];

foreach ( $csv as $row )    {
    [$date, $time] = explode("T", $row["DATE"]);
    if ( ! isset($output[$date]) )  {
        $output[$date] = $dayTemplate;
    }
    $output[$date][$time] = $row["TMP"];
}
print_r($output);

Upvotes: 1

Related Questions