Smarty: How to reference to the associative array index

前端 未结 4 809
迷失自我
迷失自我 2020-12-29 08:01
Array $imagelist:

Array ( 
    [additional] => Array ( 
        [count] => 2 
        [image] => Array ( 
            [nokia_e61_1.jpg] => Array ( 
             


        
相关标签:
4条回答
  • 2020-12-29 08:43

    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

    0 讨论(0)
  • 2020-12-29 08:44
    {php}echo $imagelist['additional']['image'][$getvars['imagename']]['name_body'];{/php}
    
    0 讨论(0)
  • 2020-12-29 08:53

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

    0 讨论(0)
  • 2020-12-29 09:01

    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}
    
    0 讨论(0)
提交回复
热议问题