如何在foreach循环中分离数据列


How to get data column separated in foreach loop

如何在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 
$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 />";
        }
    }
}

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

把钥匙当作时间(今天,明天,过期)。

这应该是一个多维数组。

循环打印如下:

<?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;;
      } 
    }   
  }
}
?>

试试这个

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 

根据您的数据结构:

$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