【问题标题】:How to get data column separated in foreach loop如何在foreach循环中分离数据列
【发布时间】:2016-01-29 11:28:18
【问题描述】:

如何在 foreach 循环的帮助下获取今天、即将到来和过期日期等不同部分的数据。

foreach($list as $index=>$row) {                                 
    $dueDt = new DateTime($row->due_timeDate);
    $todayDt = new DateTime('now');
    $due = $dueDt->format('Y-m-d');   
    $today = $todayDt->format('Y-m-d');  

    if($due == $today) {                               

        echo $due.' Today' ;
        echo '<br>';
        $task['today'][]=$row;

    } elseif($due>$today) {

        echo $due.' Upcoming'; 
        echo '<br>'; 
        $task['today'][]=$row;

    } elseif($due<$today) {

        echo $due.' Overdue';; 
        echo '<br>';   
        $task['overdue'][]=$row;
    } 
}

我的数据是这样来的:

2015-10-30 Upcoming
2015-10-29 Today
2015-10-28 Overdue
2015-10-28 Overdue
2015-10-27 Overdue
2015-10-27 Overdue
2015-10-15 Overdue

但我想从 foreach 循环中得到这样的数据:

Upcoming
        2015-10-30 

Today
        2015-10-29 
Overdue
        2015-10-28 
        2015-10-28 
        2015-10-27 
        2015-10-27 
        2015-10-15 

【问题讨论】:

    标签: php codeigniter for-loop foreach while-loop


    【解决方案1】:

    根据您的数据结构:

    $data = ['2015-10-25','2015-10-26','2015-10-27','2015-10-28','2015-10-29','2015-10-30'];
    $current = '2015-10-27';
    
    // Saving Array Keys for future work
    $rows = array('overdue','today','upcomming');
    
    foreach($data as $date) {
        // Compare each date to find our $rows array
            $curt = strtotime($current);
            $d = strtotime($date);
            if($d < $curt) {
                $rows['overdue'][] = $date;
            } else if($d > $curt) {
                $rows['upcomming'][] = $date;
            } else if($d == $curt) {
                $rows['today'][] = $date;
            }
        }
    
    //Now Print each $rows:
    echo 'Overdue: <br>'. implode('<br>', $rows['overdue']) .'<br><br>';
    echo 'Today: <br>'. implode('<br>', $rows['today']) .'<br><br>';
    echo 'Upcomming: <br>'. implode('<br>', $rows['upcomming']);
    

    打印:

    Overdue: 
    2015-10-25
    2015-10-26
    
    Today: 
    2015-10-27
    
    Upcomming: 
    2015-10-28
    2015-10-29
    2015-10-30
    

    【讨论】:

      【解决方案2】:

      试试这个

      foreach($list as $index=>$row) { 
      
          $dueDt = new DateTime($row->due_timeDate);
          $todayDt = new DateTime('now');
          $due=$dueDt->format('Y-m-d');   
          $today=$todayDt->format('Y-m-d');  
      
          if($due==$today){
      
              $up['upcome']=$row;
      
          }elseif($due>$today){
      
              $to['Today']=$row;
      
          }elseif($due<$today){
      
              $over['overdue']=$row;
          }
      
          if (!empty($up)) {
              echo "Upcoming";
            foreach ($up as $new_up) {
              echo "&nbsp;" . $new_up['upcome']."<br>";  
            }
          }
          if (!empty($to)) {
              echo "Today";
            foreach ($to as $new_to) {
              echo "&nbsp;" . $new_to['Today']."<br>";  
            }
          }
          if (!empty($over)) {
              echo "Overdue";
            foreach ($over as $new_over) {
              echo "&nbsp;" . $new_over['overdue']."<br>";  
            }
          }
      }
      

      输出将是

      Upcoming
              2015-10-30 
      
      Today
              2015-10-29 
      Overdue
              2015-10-28 
              2015-10-28 
              2015-10-27 
              2015-10-27 
              2015-10-15 
      

      【讨论】:

      • @Amit 你检查了吗??
      【解决方案3】:

      您需要使用数组的键值对。

      以关键为时间(今天、明天、逾期)。

      这应该是一个多维数组。

      循环打印如下:

      <?php
      $tasks = array();
      foreach($list as $index=>$row) {
        $dueDt = new DateTime($row->due_timeDate);
        $todayDt = new DateTime('now');
        $due=$dueDt->format('Y-m-d');
        $today=$todayDt->format('Y-m-d');
        if($due==$today) {
          $tasks['today'][] = $due;
        }
        else if($due>$today) {
          $tasks['upcoming'][]=$due;
        }
        else if($due<$today) {
          $tasks['overdue'][]=$due;
        }
      }
      if (! empty($tasks)) {
        foreach ($tasks as $dueDate => $task) {
          echo ucwords($dueDate);
          if (! empty($task)) {
            foreach ($task as $title) {
              echo "<br/>--> " . $title;;
            } 
          }   
        }
      }
      ?>
      

      【讨论】:

        【解决方案4】:
        $task['today'] = array();
        $task['Upcoming'] = array();
        $task['Overdue'] = array();
        
        foreach ($list as $index => $row)
        {
            $dueDt = new DateTime($row->due_timeDate);
            $todayDt = new DateTime('now');
            $due = $dueDt->format('Y-m-d');
            $today = $todayDt->format('Y-m-d');
            if ($due == $today)
            {
                $task['today'][] = $row;
            }
            elseif ($due > $today)
            {
                $task['Upcoming'][] = $row;
            }
            elseif ($due < $today)
            {
                $task['Overdue'][] = $row;
            }
        }
        foreach ($tasks as $dueDate => $task)
        {
            if (!empty($task))
            {
                echo $dueDate . "<br />";
        
                foreach ($task as $date)
                {
                    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $date . "<br />";
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多