dongxuying7583 2013-07-31 18:47
浏览 33
已采纳

具有变量值的关联数组

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

  • 写回答

2条回答 默认 最新

  • dpgf42422 2013-07-31 19:01
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题