PHP:使用 foreach 访问多维数组并将结果输出为表格 [英] PHP: Accessing multidimensional array with foreach and outputting the results as table

查看:78
本文介绍了PHP:使用 foreach 访问多维数组并将结果输出为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been stuck on this coding for quite some time now. I'm new to PHP, so I might've made a stupid mistake somewhere. My aim is to show a table of attendance for a month after a user selects the date and year. Similar to this

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

这是我正在谈论的代码:

This is the code I'm talking about:

<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) 看起来像这样

var_dump($dome) looks like this

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、使用键等.我确信这段代码看起来很乱,但现在我很难过.

Despite reading many guides here about accessing multidimensional array, I still can't understand how to use foreach, using keys and such. I'm sure this code looks really messy but right now I'm stumped.

我希望有人能帮助我.

找到重复问题的解决方案

Found the solution to the the duplicate problem

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

Another problem I'm having is that I'm not sure my way of accessing the array is correct. By the end of the code, $staff should look like this:

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 以便将时间从后者提取到前者.但我不确定我是否正确地执行了 foreach 来实现这一目标.

I'll need to loop through $staff and $dome in order to extract the time from the latter into the former. But I'm not sure I'm doing the foreach correctly to achieve that.

推荐答案

抱歉,因为我读错了你的代码而进行编辑.

Apologies, editing as I read your code wrong.

看起来您正在为变量 $staff 赋值,而不是将该值填充到 $staff 的现有数组中.

It looks like you are assigning a value to the variable $staff, instead of stuffing that value into the existing array at $staff.

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

应该是:

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

这篇关于PHP:使用 foreach 访问多维数组并将结果输出为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆