我一直在到处寻找我需要的东西,但我无处可去。不知道是不是因为我想要的东西不被允许,或者我只是不知道如何正确地用词来找到解决方案。
我正在创建一个包含很多工作表的 excel 导出,所以有很多重复的代码,所以我做了一个 foreach 循环。问题是我的数组里面有变量,我需要代码把这些特定的变量放在我需要的地方。
$y1953 =& $workbook->add_worksheet('1953');
$y1958 =& $workbook->add_worksheet('1958');
$y1963 =& $workbook->add_worksheet('1963');
$y1978 =& $workbook->add_worksheet('1978');
$y1988 =& $workbook->add_worksheet('1988');
$y2003 =& $workbook->add_worksheet('2003');
$yearlist = array($y1953 => '1953', $y1958 => '1958', $y1963 => '1963', $y1978 => '1978', $y1988 => '1988', $y2003 => '2003');
foreach ($yearlist as $year => $yearnum){
$line = 1; # Start on line 2
# Write each line
foreach ($bll->rows AS $row) {
$numberlist = array (''=>'1', '2'=>'2', '3' => '3', '4' => '4');
foreach ($numberlist as $name => $num){
if ($row['gYear'.$name] === $yearnum){
$col = 0;
$year->write_string($line, $col, ''); $col += 1;
$year->write_string($line, $col, $row['name_last'.$name]);$col += 1;
$year->write_string($line, $col, $row['name_first'.$name]); $col += 1;
$year->write_string($line, $col, $row['name_last']); $col +=1;
if($row['session'. $num .'1'] === '1')
{
$year->write_number($line, $col, $row['session'. $num .'1'] );
}$col += 1;
if($row['session'. $num .'2'] === '1')
{
$year->write_number($line, $col, $row['session'. $num .'2'] );
}$col += 1;
if($row['session'. $num .'3'] === '1')
{
$year->write_number($line, $col, $row['session'. $num .'3'] );
}$col += 1;
if($row['session'. $num .'4'] === '1')
{
$year->write_number($line, $col, $row['session'. $num .'4'] );
}$col += 1;
if($row['session'. $num .'5'] === '1')
{
$year->write_number($line, $col,$row['session'. $num .'5'] );
}$col += 1;
if($row['session'. $num .'6'] === '1')
{
$year->write_number($line, $col, $row['session'. $num .'6'] );
}$col += 1;
$year->write_number($line, $col, '1'); $col +=1;
$year->write_string($line, $col, $row['notes']); $col += 1;
$line += 1;
}
}
}
$yearlist 是我遇到问题的数组。我需要第一个值“$y1953”作为 $year 在 foreach 循环中的位置。现在,我的 Excel 表中没有任何内容。那么有没有办法让代码把变量放在我需要的地方?或者它可能是变量值?
我会以不同的方式执行此操作,但我正在为其制作此 excel 导出的人希望以特定的方式进行操作。
谢谢