如何将这个Smarty代码转换回PHP?
据我所知,Smarty包含许多在PHP中具有等效性的内置函数 如何将下面的代码转换回本机PHP?“部分”类似于for循环吗如何将这个Smarty代码转换回PHP?,php,loops,for-loop,smarty,Php,Loops,For Loop,Smarty,据我所知,Smarty包含许多在PHP中具有等效性的内置函数 如何将下面的代码转换回本机PHP?“部分”类似于for循环吗 <table width="400" border="0"> {section name=x loop=$records} <tr> {section name=y loop=$records[x]} <td align="right"> <input type
<table width="400" border="0">
{section name=x loop=$records}
<tr>
{section name=y loop=$records[x]}
<td align="right">
<input type="checkbox" name="{$records[x][y].prefkey}" {if $records[x][y].prefval eq "on"}checked{/if} />
</td>
<td align="left">
<strong> {$records[x][y].prefkey}</strong>
</td>
{/section}
</tr>
{/section}
</table>
{section name=x loop=$records}
{section name=y loop=$records[x]}
{$records[x][y].prefkey}
{/section}
{/section}
this{section name=x loop=$records}{section}
相当于
foreach(数组_键($records)为$x){}
此{section name=x loop=$records}{section}
相当于
foreach(数组_键($records)为$x){}
下面是一个示例,为数据对象使用简单数组:
<?php
// generate some test data
$records = array(
array(
array('prefkey'=>"foo",'prefval'=>"on"),
array('prefkey'=>"bar",'prefval'=>"off"),
),
array(
array('prefkey'=>"foo",'prefval'=>"off"),
array('prefkey'=>"bar",'prefval'=>"off"),
),
array(
array('prefkey'=>"foo",'prefval'=>"off"),
array('prefkey'=>"bar",'prefval'=>"on"),
),
);
?>
<table width="400" border="0">
<?php for($x=0; $x<count($records); $x++){ ?>
<tr>
<?php for($y=0; $y<count($records[$x]); $y++){ ?>
<td align="right">
<input type="checkbox" name="<?=$records[$x][$y]['prefkey']; ?>" <?=($re
cords[$x][$y]['prefval'] == "on"? "checked" : "") ?>/></td>
<td align="left">
<strong> <?=$records[$x][$y]['prefkey']; ?></strong>
</td>
<?php }?>
</tr>
<?php }?>
</table>
以下是为数据对象使用简单数组的示例:
<?php
// generate some test data
$records = array(
array(
array('prefkey'=>"foo",'prefval'=>"on"),
array('prefkey'=>"bar",'prefval'=>"off"),
),
array(
array('prefkey'=>"foo",'prefval'=>"off"),
array('prefkey'=>"bar",'prefval'=>"off"),
),
array(
array('prefkey'=>"foo",'prefval'=>"off"),
array('prefkey'=>"bar",'prefval'=>"on"),
),
);
?>
<table width="400" border="0">
<?php for($x=0; $x<count($records); $x++){ ?>
<tr>
<?php for($y=0; $y<count($records[$x]); $y++){ ?>
<td align="right">
<input type="checkbox" name="<?=$records[$x][$y]['prefkey']; ?>" <?=($re
cords[$x][$y]['prefval'] == "on"? "checked" : "") ?>/></td>
<td align="left">
<strong> <?=$records[$x][$y]['prefkey']; ?></strong>
</td>
<?php }?>
</tr>
<?php }?>
</table>
根据文档,{section}用于顺序数组,而不是关联数组@你说得对。谢谢@Trenton Scott说,根据文档,{section}用于顺序数组,而不是关联数组,这是行不通的@你说得对。谢谢@特伦顿·斯科特,据医生说,这行不通