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

finding the KEY / VALUE of a 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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sun Nov 19, 2017 5:58 am    Post subject: finding the KEY / VALUE of a sub-array ? Reply with quote

Right now i'm doing a foreach loop.

The value of the loop is another sub-array, with ONE key/value combination.

is there a quick way of finding the key/value "values" quickly ? the only way i can figure it out is by doing another foreach loop. but this seems cumbersome.

Code:

{foreach from=$levels key=k item=v}
<tr><td>{$k}</td><td>{$stats.NAME.$k}</td><td>{$stats.NAME2.$k}</td><td>{$stats.NAME3.$k}</td></tr>
{/foreach}



$stats is ok - the key to $stats is taken from $levels. This is correct. within $stats are further keys - NAME, NAME2, NAME3,

$stats.NAME.$k produces a sub-array with one key/combintion. i do not know the "key" in advance.

is there a way, to just say "display the 1st key/value combination found"
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Nov 19, 2017 5:43 pm    Post subject: Reply with quote

Cumbersome? To whom? I would let the computer do it.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Nov 19, 2017 10:06 pm    Post subject: Reply with quote

Key-value or just value?
Also, where are stats came from, if you only have $v ?
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Mon Nov 20, 2017 4:40 am    Post subject: Reply with quote

AnrDaemon wrote:
Key-value or just value?
Also, where are stats came from, if you only have $v ?


ive got the $stats as a separate array. the keys/value combination is also the key in $stats.

in the above script - it will ALWAYS have a sub-array with one key-value set.

just wondering if theres a quick way of extracting them - without doing a foreach loop on the sub-array.

I could just do $v.0 to get the 1st value of $v - however the keys in $v are not numerical, & i cant just go $v.1 $v.2 etc.. as i do not know the keys in advance.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon Nov 20, 2017 7:51 am    Post subject: Reply with quote

You could try if Smarty will allow the use of PHP function each().
Code:
{{$v|each}[0]}
should get you the key of the array element in $v that the array pointer is currently sitting at. The value of the array element referenced is at index [1].

I haven't tried this.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Nov 20, 2017 11:50 am    Post subject: Reply with quote

each() is deprecated.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Nov 20, 2017 12:05 pm    Post subject: Reply with quote

sarahfoxnz wrote:
just wondering if theres a quick way of extracting them - without doing a foreach loop on the sub-array.

foreach is quick. All other methods are slower.
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 -> Tips and Tricks 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