带字符串键的动态数组

时间:2015-07-23 00:27:57

标签: php arrays smarty

我有阵列:

Array
(
[CS] => Array
    (
        [Zomblerz] => 1.80
        [Quintic] => 2.06
        [Mostly Harmless] => 2.21
        [Nexus eSports] => 1.70
        [WinOut.net] => 2.73
        [Ace Gaming] => 1.48
        [Luminosity Gaming] => 3.68
        [Natus Vincere] => 1.30
    )

[LoL] => Array
    (
        [Jin Air Green Wings] => 1.17
        [Incredible Miracle] => 5.40
        [Rebels Anarchy] => 1.27
        [SBENU Sonicboom] => 3.92
        [SK Telecom T1] => 1.19
        [CJ Entus] => 4.95
        [KOO Tigers] => 1.38
        [NaJin e-mFire] => 3.15
    )

[StarCraft] => Array
    (
        [Maru] => 1.48
        [Zest] => 2.73
        [Stats] => 1.90
        [ByuL] => 1.94
    )
)

我希望在Smarty中使用{section}显示我的数组,但是我有一个字符串索引键,并且无法执行此操作。 我试过{foreach},但结果太糟糕了。

{section name=i loop=$odds}
<table>
  <th> here i want game name... </th>
  {section name=j loop=$odds[i]}
  <tr>
     <td> here team name - here team number </td>
     ...
  </tr>
  {/section}
 </table>
 {/section}
你帮我吗?

P.S。抱歉我的英文(

2 个答案:

答案 0 :(得分:0)

可能有更好的方式来获得你想要的聪明。但如果你需要的只是带数字键的数组,你可以使用array_values();在数组上,它将重新索引键,

http://php.net/manual/en/function.array-values.php

答案 1 :(得分:0)

{section}仅用于循环数字索引的数组

{foreach}用于循环关联数组

Example 7.8. {foreach} with nested item and key - http://www.smarty.net/docsv2/en/language.function.foreach.tpl#id2802172

展示了您想要的结果

您的代码看起来像 -

sudo apt-get install python3-scipy

修改

根据您的评论,如果您想使用

更改表格布局
{foreach key=game item=odd from=$odds}
  <table>
  <th>{$game}</th>
  {foreach key=team item=teamnumber from=$odd}
    <tr>
      <td>{$team}: {$teamnumber}</td>
    </tr>
  {/foreach}
  </table>
{/foreach}
你可以做点什么 -

{if $smarty.foreach.teams.index % 2 == 0}...{/if}

请参阅http://www.smarty.net/docsv2/en/language.function.foreach.tpl#foreach.property.index

相关问题