|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Sun Nov 19, 2017 5:58 am Post subject: finding the KEY / VALUE of a sub-array ? |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Nov 19, 2017 5:43 pm Post subject: |
|
|
Cumbersome? To whom? I would let the computer do it. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Nov 19, 2017 10:06 pm Post subject: |
|
|
Key-value or just value?
Also, where are stats came from, if you only have $v ? |
|
Back to top |
|
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Mon Nov 20, 2017 4:40 am Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Nov 20, 2017 7:51 am Post subject: |
|
|
You could try if Smarty will allow the use of PHP function each().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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Nov 20, 2017 11:50 am Post subject: |
|
|
each() is deprecated. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Nov 20, 2017 12:05 pm Post subject: |
|
|
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 |
|
|
|
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
|