具有变量值的关联数组

I have been looking all over the place for what i need but i'm not getting anywhere. Not sure if it is because the thing i want isn't allowed or i just don't' know how to word it properly to find the solution.

I am creating a excel export that has a lot of worksheets so there is a lot of repetitive code so I made a foreach loop. The problem is the array i have has variables inside it and i need the code to put those specific variables to the spots i need it.

 $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 is the array that i am having trouble with. I need the first value "$y1953" to be where $year is in the foreach loop. Right now, nothing shows up in my excel sheet. So is there a way to have the code just put the variable in the spot i need it? Or could it be the variables values?

I would do this differently but the person I am making this excel export for wants it a specific way.

Thanks

Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

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

Try switching the keys and values around in your array declaration.

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

Than change your foreach to

foreach ($yearlist as $yearnum => $year)

Maybe, you could create a worksheet inside the loop. Instead of:

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

Try this:

$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
}