2011-05-12 5 views
2

J'ai un tableauindex de tableau Smarty

array('foo_1' => 1, 'bar_1' =>1, 'foo_2' => 2, 'bar_2' => 2, ...) 

for($i = 1; $i<=12; $i++){ 
    echo $month['foo_'.$i]; 
} 

Et je ne comprends pas comment puis-je accéder aux valeurs de l'aide Smarty.

{counter start=0 skip=1 assign="i"} 
{section name = month start = 0 loop = 12 step = 1} 
    {if isset($arr.foo_.$i)} 
    {$arr.bar_$i} 
    {/if} 
    {counter} 
{/section} 

Mais cela ne fonctionne pas. Le principal problème est - Comment puis-je accéder à array ['foo_1'] dans smarty? Peux-tu aider s'il te plait?

+0

Je ne suis pas un programmeur PHP, mais n'est-ce pas un hachage et pas un tableau? – zellio

+0

@Mimisbrunnr un HashTable est appelé un tableau en PHP;) – elslooo

+0

Voulez-vous dire une table de hachage? En PHP il n'y a pas de tables de hachage comme en JAVA. Cependant, un tableau est une sorte de table de hachage n'est-ce pas? – alagar

Répondre

2

Vous devez générer la clé avant de l'accéder au tableau. Utilisez assign pour créer le nom complet.

{assign var=fooKey value="foo_"|cat:$i} 
{assign var=barKey value="bar_"|cat:$i} 
{if isset($arr.{$fooKey})} 
    {$arr.{$barKey}} 
{/if} 

Votre tentative {$arr.foo_.$i} moyens d'accès à la sous-clé $i dans le tableau sous $arr.foo_.