1. 程式人生 > >thinkphp5模板輸出二維陣列

thinkphp5模板輸出二維陣列

在專案開發中遇到從控制器向模板傳入二維陣列並輸出問題,當從控制器傳入二維陣列時,可以用for語句進行輸出具體如下:

控制器方法:

public function showBoundary(){

    $numbers=input('get.numbers');
    $boundarynt=array();
    if($numbers<>''){
      $boundary=Db::name('admingridata')->where('numbering',$numbers)->value('boundary');
     $boundtype=explode(';',$boundary);
    // print_r($boundtype);
    $countArray=count($boundtype);
    for($i=0;$i<$countArray;$i++){
      $boundaryMix=$boundtype[$i];
      $bmix=explode(" ",$boundaryMix);
      $boundarynt['bname'][$i]=$bmix[1];
      $boundarynt['btype'][$i]=$bmix[0];
    }
    //print_r($boundarynt['bname']);
    //print_r($boundarynt);
    $boundaryname=$boundarynt['bname'];
    print_r($boundaryname);
    $boundarytype=$boundarynt['btype'];
    }
    $this->assign('countg',$countArray);
    $this->assign('boundarynt',$boundarynt);
    $this->assign('boundaryname',$boundaryname);
    $this->assign('boundarytype',$boundarytype);
      return $this->fetch('showboundary');
  }

模板輸出程式碼:

{$countg}
<table>
{for start="0" end="$countg"}
<tr>
<td>{$boundarynt['bname'][$i]}</td>
<td>{$boundarynt['btype'][$i]}</td></tr>
{/for}
</table>