PHP:使用foreach访问多维数组并将结果输出为表

时间:2015-07-23 17:55:24

标签: php multidimensional-array

我已经在这段编码上坚持了很长一段时间。我是PHP的新手,所以我可能在某个地方犯了一个愚蠢的错误。我的目的是在用户选择日期和年份后显示一个月的出勤表。与此相似

   January 2015
   Name              |1   |2   |3|...|31|
   STAFF A-Full Name |9:02|8:30| |...|  |
   STAFF B-Full Name |8:43|    | |...|  |

这是我正在谈论的代码:

<html>
<?php
error_reporting(0);
//database connection goes here

$m = $_POST['month'];
$y = $_POST['year'];

$inidate= print_r($y.'/'.$m.'/%',true);

$sql = "SELECT NAME, DATEIN, TIMEIN FROM VXDTIME WHERE NAME != '' AND NAME LIKE '%-%' AND NAME NOT LIKE '%Old% %Data%' AND TIMEIN != '' ORDER BY NAME, DATEIN"; 

$result = ibase_query($connect,$sql);

$staff = $dome = array();
$getmonth = date("m",strtotime($inidate));
$getyear = date("Y",strtotime($inidate));

while($row = ibase_fetch_assoc($result))
{   $dome[] = $row; }

foreach($dome as &$value)
{
    if (ctype_upper(substr($value['NAME'],0,2))== TRUE)
        $staff = $value['NAME'];
}
$staff = array_values(array_unique($staff,SORT_REGULAR));

//***************************
foreach ($staff as $key1 => $value1)
{
    foreach ($dome as $key => $value)
    {
        for ($i=0;$i<cal_days_in_month(CAL_GREGORIAN,$getmonth,$getyear);$i++)
        {
            if ($value1 == $key['NAME']) //compares name
                if ($key['DATEIN'] != NULL) //compares date
                    if (idate("d",strtotime($key['DATEIN'])) == (i+1))
                         $key1[$i+1] = $key['TIMEIN'];
                    else
                         $key1[$i+1] = 'No Record';
                else
                    $key1[$i+1] = 'Blank';
        }
    }
}

//Make the array start at 0
//$staff = array_values($staff);
//array_walk($staff, create_function('&$v,$k', 'if (is_array($v)) $v=array_values($v);'));

$getdate = print_r($y.'/'.$m.'/01',true);
echo "<table border=1><tr><td>No.</td> <td style='width:350'>Name</td>";

for($i=0;$i<cal_days_in_month(CAL_GREGORIAN,$getmonth,$getyear);$i++)
    echo "<td style='width:40'>".($i+1)."</td>";
echo "</tr>";

$count=0;
foreach ($staff as $key => $value)
{       
    echo "<tr><td>".($count+1)."</td><td>".$key[0]."</td>";

    for($j=1;$j<(cal_days_in_month(CAL_GREGORIAN,$getmonth,$getyear)+1);$j++)
    {           
        if (date("D",strtotime($getdate)) == 'Sat' || date("D",strtotime($getdate)) == 'Sun')
            echo "<td BGCOLOR='#525266'> </td>";
        else
            if (strtotime($key[$j]) > strtotime('09:10'))
                echo "<td BGCOLOR='#ffff00'>".$key[$j]."</td>";
            else
                echo "<td>".$key[$j]."</td>";
    }
    echo "</tr>";
    $getdate=strftime("%Y/%m/%d", strtotime("$getdate +1 day"));
    count++;
}
echo "</table>";
?>
</html>

var_dump($ dome)看起来像这样

array
  0 => 
    array
      'NAME' => string 'STAFF A-Full Name'
      'DATEIN' => string '2015/01/01' //string date/time isn't not my choice
      'TIMEIN' => string '09:02'
  1 => 
    array
      'NAME' => string 'STAFF A-Full Name'
      'DATEIN' => string '2015/01/02'
      'TIMEIN' => string '08:30'
  2 => 
    array
      'NAME' => string 'STAFF B-Full Name'
      'DATEIN' => string '2015/01/01'
      'TIMEIN' => string '08:43'
  3 => 
    array
      'NAME' => string 'Staff B-Full Name'
      'DATEIN' => string '2012/01/01'
      'TIMEIN' => string '09:11'
 //and so on...

尽管在这里阅读了许多关于访问多维数组的指南,但我仍然无法理解如何使用foreach,使用密钥等。我确定这段代码看起来很乱,但现在我很难过。

我希望有人可以帮助我。

编辑:找到重复问题的解决方案

我遇到的另一个问题是,我不确定我访问阵列的方式是否正确。在代码结束时,$ staff应如下所示:

array
  0 => 
    array
      'NAME' => string 'STAFF A-Full Name'
      0 => string '09:02'
      1 => string '08:30'
      2 => string '09:00'
      //... all the way to the end of month

  1 => 
    array
      'NAME' => string 'STAFF B-Full Name'
      0 => string '08:43'
      1 => string '09:01'
      2 => string '08:50'
      //...
  //...

我需要遍历$ staff和$ dome以便将时间从后者提取到前者。但是我不确定我是否正确地做到了这一点。

1 个答案:

答案 0 :(得分:0)

道歉,在我读错你的代码时进行编辑。

看起来您正在为变量$ staff分配一个值,而不是将该值填充到$ staff的现有数组中。

foreach($dome as &$value)
{
  $staff = $value['NAME'];
}
$staff = array_unique($staff);

应该是:

foreach($dome as &$value)
{
  $staff[] = $value['NAME'];
}
$staff = array_unique($staff);