php Smarty:如何引用关联数组索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/648042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:22:30  来源:igfitidea点击:

Smarty: How to reference to the associative array index

phparraysindexingreferencesmarty

提问by Edoardo Vacchi

Array $imagelist:

Array ( 
    [additional] => Array ( 
        [count] => 2 
        [image] => Array ( 
            [nokia_e61_1.jpg] => Array ( 
                [name_body] => nokia_e61_1 
                [name_ext] => jpg 
            ) 
            [nokia_e61_2.jpg] => Array ( 
                [name_body] => nokia_e61_2 
                [name_ext] => jpg 
            ) 
            [nokia_e61_3.jpg] => Array ( 
                [name_body] => nokia_e61_3 
                [name_ext] => jpg 
            ) 
            [nokia_e61_4.jpg] => Array ( 
                [name_body] => nokia_e61_4 
                [name_ext] => jpg 
            ) 
        ) 
    ) 
    [main] => nokia_e61 
) 

The value nokia_e61_1.jpgis kept in {$getvars.imagename}.

该值nokia_e61_1.jpg保留在{$getvars.imagename}.

I wrote {$imagelist.additional.image.`$getvars.imagename`.name_body}but it doesn't work.

我写了{$imagelist.additional.image.`$getvars.imagename`.name_body}但它不起作用。

Please help.

请帮忙。

回答by Edoardo Vacchi

see if {$imagelist.additional.image[$getvars.imagename].name_body}works

看看是否{$imagelist.additional.image[$getvars.imagename].name_body}有效

回答by michal kralik

I don't like smarty for this, nevertheless I use it. Here's extract form documentation

我不喜欢 smarty,但我使用它。这是提取表格文档

{$foo.$bar}   <-- display variable key value of an array, similar to PHP $foo[$bar]

To be able to do it, you have to

为了能够做到这一点,你必须

{assign var='key' value=$getvars.imagename}
{$imagelist.additional.image.$key.name_body}

Hope it helps

希望能帮助到你

回答by karim79

Restructure the array, the keys for the inner 'images' offset are redundant anyway:

重组数组,内部“图像”偏移的键无论如何都是多余的:

$imagelist = array('additional' => array('count' => 2,
       'image' => array(
            array('name_body' => 'nokia_e61_1',
                  'name_ext'  => 'jpg'),
            array('name_body' => 'nokia_e61_2',
                  'name_ext'  => 'jpg'),
            array('name_body' => 'nokia_e61_3',
                  'name_ext'  => 'jpg'),
            array('name_body' => 'nokia_e61_4',
                  'name_ext'  => 'jpg')
        )
    ),
    'main' => 'nokia_e61'
);

Gives you numerically indexed images:

为您提供数字索引图像:

array(2) {
  ["additional"]=>
  array(2) {
["count"]=>
int(2)
["image"]=>
array(4) {
  [0]=>
  array(2) {
    ["name_body"]=>
    string(11) "nokia_e61_1"
    ["name_ext"]=>
    string(3) "jpg"
  }
  [1]=>
  array(2) {
    ["name_body"]=>
    string(11) "nokia_e61_2"
    ["name_ext"]=>
    string(3) "jpg"
  }
  [2]=>
  array(2) {
    ["name_body"]=>
    string(11) "nokia_e61_3"
    ["name_ext"]=>
    string(3) "jpg"
  }
  [3]=>
  array(2) {
    ["name_body"]=>
    string(11) "nokia_e61_4"
    ["name_ext"]=>
    string(3) "jpg"
  }
}
}
["main"]=>
string(9) "nokia_e61"
}

//and then the smarty bit
{$imagelist.additional.image[0].name_body}
{$imagelist.additional.image[1].name_body}
{$imagelist.additional.image[2].name_body}
{$imagelist.additional.image[3].name_body}

回答by strager

{php}echo $imagelist['additional']['image'][$getvars['imagename']]['name_body'];{/php}