Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这个Smarty代码转换回PHP?_Php_Loops_For Loop_Smarty - Fatal编程技术网

如何将这个Smarty代码转换回PHP?

如何将这个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

据我所知,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="checkbox" name="{$records[x][y].prefkey}" {if $records[x][y].prefval eq "on"}checked{/if} />
        </td>
        <td align="left">
            <strong>&nbsp;{$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>&nbsp;<?=$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>&nbsp;<?=$records[$x][$y]['prefkey']; ?></strong>                               
        </td>                                                                                       
        <?php }?>                                                                                   
</tr>                                                                                               
<?php }?>                                                                                           
</table>


根据文档,{section}用于顺序数组,而不是关联数组@你说得对。谢谢@Trenton Scott说,根据文档,{section}用于顺序数组,而不是关联数组,这是行不通的@你说得对。谢谢@特伦顿·斯科特,据医生说,这行不通