【问题标题】:Smarty current on associative array关联阵列上的 Smarty 电流
【发布时间】:2010-10-05 06:57:24
【问题描述】:

我有一个关联数组,我正在尝试使用 smarty 获取第一条记录。

在 php 中我使用 current(array),但 smarty 似乎没有 current。

所以我写了下面的代码

{if is_array($browseProducts.data) }
    <ul class="products-grid">

        {foreach from=$browseProducts.data item=item}
            {assign var='image' value=''}
            {if is_array($item.images) }
                {php} $image=current($item.images); {/php} 
            {/if}   
        {/foreach}

    </ul>
{/if}

在 {php} 部分 current($item.images) 给出警告:current() [function.current]: Passed variable is not an array or object

语法是正确的,所以我猜来自 smarty 的 $item.images 不能被 {php} 读取

将 $item.images 传递给 {php} 部分的任何方式,或者什么。

有什么建议可以解决我的问题吗?

【问题讨论】:

    标签: smarty


    【解决方案1】:

    但是如果数组的键是关联的呢?你也可以这样做:

    {$item.images|@current}
    

    【讨论】:

      【解决方案2】:
      {$item.images.0}
      

      应该返回 $item.images 数组的第一个元素。

      所以:

      {if is_array($browseProducts.data) }
          <ul class="products-grid">
      
              {foreach from=$browseProducts.data item=item}
                  {assign var='image' value=''}
                  {if is_array($item.images) }
                      {$item.images.0}
                  {/if}   
              {/foreach}
      
          </ul>
      {/if}
      

      【讨论】:

        猜你喜欢
        • 2012-10-07
        • 2014-02-16
        • 2016-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-19
        • 2012-04-18
        • 1970-01-01
        相关资源
        最近更新 更多