0

我一直在到处寻找我需要的东西,但我无处可去。不知道是不是因为我想要的东西不被允许,或者我只是不知道如何正确地用词来找到解决方案。

我正在创建一个包含很多工作表的 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 导出的人希望以特定的方式进行操作。

谢谢

4

2 回答 2

1

数组和对象不能用作键。这样做会导致警告:非法偏移类型。

http://php.net/manual/en/language.types.array.php

尝试在数组声明中切换键和值。

$yearlist = array('1953' => $y1953, '1958' => $y1958......etc.

比将您的 foreach 更改为

foreach ($yearlist as $yearnum => $year)
于 2013-07-31T19:01:05.043 回答
0

也许,您可以在循环内创建一个工作表。代替:

 $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');

试试这个:

$yearlist = array('1953', '1958', '1963', '1978', '1988', '2003');
foreach($yearlist as $value)
{
    $sheet =& $workbook->add_worksheet($value);
    //do the job with the year and new worksheet
}
于 2013-07-31T18:51:30.810 回答