Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Trouble assigning SUB array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
gordonisnz
Smarty Regular


Joined: 05 Sep 2009
Posts: 70

PostPosted: Tue May 25, 2021 11:41 am    Post subject: Trouble assigning SUB array Reply with quote

Hello. Is there a good example of how to assign a SUB array within a smarty template ?

PHP code :-

Code:
$array["family"]["Jones"]["name"]="bob";


But I want to do this within Smarty template (the array "family" doesn't exist at all. )


All the codes / examples i see online assume you only want to assign a top-level array - But I cannot find any examples for sub-arrays.

I've tried a few combinations EG :-

Code:
{if !(isset($outerform.$k.TYPEOPTION))}{assign $outerform[$k]["TYPEOPTION"]=""}{/if}



Can anyone give me JUST the smarty template coding. Thanks.
Back to top
View user's profile Send private message
gordonisnz
Smarty Regular


Joined: 05 Sep 2009
Posts: 70

PostPosted: Thu May 27, 2021 5:33 am    Post subject: Reply with quote

Has anyone got a solution ?

Ive fixed my original problem - but now gettinG UNDEFINED INDEX TYPEOPTION


Code:

{if !(isset($outerform.$k.TYPEOPTION))}{assign $outerform.$k.TYPEOPTION value=""}{/if}


THAT is why im testing for it - if its not set - then set it.

Why am i getting the error ?

Quote:

[27-May-2021 17:28:36 Pacific/Auckland] PHP Notice: Undefined index: TYPEOPTION in .....



The template has only 3 lines where TYPEOPTION exists & the first line is setting it if it doesnt exist.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun May 30, 2021 8:02 am    Post subject: Reply with quote

I don't understand your problem.

Code:
PHP: {$smarty.const.PHP_VERSION}
Smarty: {$smarty.version}

{$arr['x']['y']['z']="t"}
{$arr|print_r:true}

Code:
PHP: 7.4.16
Smarty: 3.1.36

Array
(
    [x] => Array
        (
            [y] => Array
                (
                    [z] => t
                )

        )

)


If you only want to set it if it's not set, then

Code:
PHP: {$smarty.const.PHP_VERSION}
Smarty: {$smarty.version}

{$arr['x']['y']=['z' => 't']}
{* Assign only if not set *}
{$arr['x']['y']=$arr['x']['y'] + ['z' => "d"]}
{$arr|print_r:true}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP