如何在表smarty php中显示数组值

时间:2017-02-07 19:39:46

标签: php arrays smarty

我有阵列:

$kraje = Array(
      array('Polska', 'Anglia', 'Litwa', 'Francja'),
      array('Tunezja', 'Egipt', 'RPA', 'Etiopia'),
      array('Chiny', 'Mongolia', 'Japonia', 'Kazachstan')
);

我想在smarty index.tpl的html表中或者在php中显示它。我想要的结果:

http://codepen.io/anon/pen/LxJpaV

<table style="">
  <tr>
    <th>array1</th>
    <th>array2</th> 
    <th>array3</th>
  </tr>
  <tr>
    <td>Polska</td>
    <td>Tunezja</td> 
    <td>Chiny</td>
  </tr>
  <tr>
    <td>Anglia</td>
    <td>Egipt</td> 
    <td>Mongolia</td>
  </tr>
  <tr>
    <td>Litwa</td>
    <td>RPA</td> 
    <td>Japonia</td>
  </tr>  
  <tr>
    <td>Francja</td>
    <td>Etiopia</td> 
    <td>Kazachstan</td>
  </tr>   
</table>

1 个答案:

答案 0 :(得分:1)

index.php

require("vendor/autoload.php");
$smarty = new Smarty();

$kraje = Array(
      array('Polska', 'Anglia', 'Litwa', 'Francja'),
      array('Tunezja', 'Egipt', 'RPA', 'Etiopia'),
      array('Chiny', 'Mongolia', 'Japonia', 'Kazachstan')
);
$i = new MultipleIterator();
foreach ($kraje as $a) $i->attachIterator (new ArrayIterator ($a));

$smarty->assign('name', $i);
$smarty->display("sample.tpl");

sample.tpl

<table style="">
  <tr>
    <th>array1</th>
    <th>array2</th> 
    <th>array3</th>
  </tr>
  {foreach $name as $item}
  <tr>
     {foreach $item as $record}
     <td>{$record}</td>
     {/foreach}
  </tr>
  {/foreach}
</table>