复制以前的值并将它们分配给最新日期

时间:2013-07-24 13:16:54

标签: php

问题

如果值不为NULL,我想在最新日期显示以前的日期记录。我尝试了几件事,但没有真正奏效。以下是代码,实际和期望的结果。

逻辑上我想复制过去的日期值并将它们分配到最新日期。任何帮助都会很棒。在此先感谢!!

代码

$date = date('m/d/Y',time()+( 1 - date('w'))*24*3600);
$ts = strtotime($date);            
$year1 = date('o', $ts);
$week = date('W', $ts);
$date_time_array1 = '';
for($i = 1; $i <= 7; $i++) {
   $ts = strtotime($year1.'W'.$week.$i);
   $date_time_array1[] = date('d/m/Y', $ts);
}//end for week dates

$gadget_data_type_date1 = array();
$empty = true;
for($i=0;$i<count($date_time_array1);$i++){
  $flag = 0;
  for($j=0;$j<count($date_time_array);$j++){
  if($date_time_array1[$i] == $date_time_array[$j]){
    $gadget_data_type_date1[] = $date_time_array1[$i];
    $gadget_data_type_value1[] = $gadget_data_type_value[$j];
    $flag =1;
  }
}
if(!$flag){
  $gadget_data_type_date1[] = $date_time_array1[$i];
}

 $date_time = $gadget_data_type_date1[$i];
    $t = explode("/",$date_time);
    if (mktime(0,0,0,$t[1],$t[0],$t[2]) >= $game_starts_on) {
      $date1 = date('m/d/Y',strtotime(str_replace("/",".",".$date_time.")));
      $dateOneMonthSubtracted = date('n/j/Y', strtotime($date1));
      $date_time_parse = "'".$dateOneMonthSubtracted.' UTC'."'";

      if($gadget_data_type_value1[$i] != 'null'){
        $gadget_data_type_value_value2[$i] = $gadget_data_type_value1[$i];
      }else{
        $gadget_data_type_value_value2[$i] = 'null';
      }//end if check null

      if ($gadget_data_type_value_value2[$i] != 'null' && $gadget_data_type_value_value2[$i] != '') {
        $empty = false;
      }//end if value is blank

    $date_time_array2[] = "[Date.parse(".$date_time_parse."), ".$gadget_data_type_value_value2[$i]." ]";

    }//end if current date is greater than start date

实际结果

[0] => [Date.parse('7/22/2013 UTC'), null ], i=>0
[1] => [Date.parse('7/23/2013 UTC'), 6.8717882122446 ], i=>1
[2] => [Date.parse('7/24/2013 UTC'), 0.3531183025553 ], i=>2
[3] => [Date.parse('7/24/2013 UTC'), 0.070564096031649 ], i=>3
[4] => [Date.parse('7/25/2013 UTC'), null ], i=>4
[5] => [Date.parse('7/26/2013 UTC'), 4.374864096031649 ], i=>5
[6] => [Date.parse('7/26/2013 UTC'), 1.263764096031649 ], i=>6
[7] => [Date.parse('7/27/2013 UTC'), null ], i=>7

期望的结果

[0] => [Date.parse('7/22/2013 UTC'), null ], i=>0
[1] => [Date.parse('7/23/2013 UTC'), 6.8717882122446 ], i=>1
[2] => [Date.parse('7/24/2013 UTC'), 6.8717882122446 ], i=>2
[3] => [Date.parse('7/24/2013 UTC'), 0.3531183025553 ], i=>3
[4] => [Date.parse('7/24/2013 UTC'), 0.070564096031649 ], i=>4
[5] => [Date.parse('7/25/2013 UTC'), null ], i=>5
[6] => [Date.parse('7/26/2013 UTC'), 6.8717882122446 ], i=>6
[7] => [Date.parse('7/26/2013 UTC'), 0.3531183025553 ], i=>7
[8] => [Date.parse('7/26/2013 UTC'), 0.070564096031649 ], i=>8
[9] => [Date.parse('7/26/2013 UTC'), 4.374864096031649 ], i=>9
[10] => [Date.parse('7/26/2013 UTC'), 1.263764096031649 ], i=>10
[11] => [Date.parse('7/27/2013 UTC'), null ], i=>11

2 个答案:

答案 0 :(得分:1)

NULL应该没有引号:

if ($gadget_data_type_value1[$i] != NULL)

如果您将NULL括在引号'NULL'中,它就会成为一个字符串。


如果您只是检查值是否为空,则可以使用empty()功能。

if (empty($gadget_data_type_value1[$i]))

答案 1 :(得分:0)

你可以在下面进行循环;

 <?php
 $len = 1; // the amount of your list;
 $prev_list = array();
 for ($i = 0; $i < $len; $i++)
 {
     $date_time_parse = ''; // should like: '7/22/2013 UTC'
     $value = $gadget_data_type_value_value2[$i];

     // if value is not NULL
     if ($value != null && $value != '')
     {
         // all previous value before this date
         foreach ($prev_list as $pre_value)
         {
             $date_time_array2[] = "[Date.parse(".$date_time_parse."), " . $pre_value . " ]";
         }
         // add value of this date to the $prev_list
         $prev_list[] = $value;
     }
     else
     {
         $value = 'null';
     }
     // add the date's value
     $date_time_array2[] = "[Date.parse(".$date_time_parse."), " . $value . " ]";
 }