Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Read multidimensional array in smarty

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
HockS
Smarty n00b


Joined: 08 Nov 2019
Posts: 3

PostPosted: Fri Nov 08, 2019 10:40 am    Post subject: Read multidimensional array in smarty Reply with quote

Hi, I have a problem reading an array in the loop.

I have the following variable:

    variable: $shop.hours
    value: [["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14"],["Close"]] [note] =>


When I try to read it in the loop, it returns the result of the complete variable.

Example:

Code:
{foreach item=timetable from=$shop.hours}
   {$timetable}
{/foreach}


This is what returns me: [["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14"],["Close"]] [note] =>

I am sorry for my bad english.

Edit: I was wrong, the correct value of the variable is [["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14"],["Close"]]


Last edited by HockS on Mon Nov 11, 2019 8:00 am; edited 1 time in total
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1577

PostPosted: Fri Nov 08, 2019 7:40 pm    Post subject: Reply with quote

Code:
{$shop.hours|var_dump}

??
Back to top
View user's profile Send private message
HockS
Smarty n00b


Joined: 08 Nov 2019
Posts: 3

PostPosted: Mon Nov 11, 2019 7:56 am    Post subject: Reply with quote

AnrDaemon wrote:
Code:
{$shop.hours|var_dump}

??


This is the dump: string(134) "[["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14 \/ 16 - 20"],["9 - 14"],["Close"]]"
Back to top
View user's profile Send private message
HockS
Smarty n00b


Joined: 08 Nov 2019
Posts: 3

PostPosted: Mon Nov 11, 2019 9:27 am    Post subject: Reply with quote

I created a temporary patch ... it works, but I think it's not the right way. I would like it to work with smarty alone.

Code:

<script>
    var timetable = {$shop.hours nofilter};
   
    {literal}

    for (var i = 0; i < timetable.length; i++) {
        document.write(timetable[i]);
    }

   {/literal}
</script>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1577

PostPosted: Fri Nov 15, 2019 9:43 pm    Post subject: Reply with quote

Quote:
This is the dump: string(134)

It's a string, not an array. So, nothing to read from.
If you think that's not right, you could try to use json_decode on it.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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