简体   繁体   中英

Smarty: How to reference to the associative array index

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.jpg is kept in {$getvars.imagename} .

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

Please help.

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

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

{$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

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}
{php}echo $imagelist['additional']['image'][$getvars['imagename']]['name_body'];{/php}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM