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

Find array value without foreach?

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


Joined: 05 Sep 2009
Posts: 69

PostPosted: Sat May 08, 2021 7:27 am    Post subject: Find array value without foreach? Reply with quote

Hi, Ive got a query regarding getting data from an array.

DATA1 >> day >> 5
DATA1 >> month >> 3
DATA1 >> year >> 2020

DATA2 >> day >> 5
DATA2 >> month >> 3
DATA2 >> year >> 2020

DATA3 >> day >> 5
DATA3 >> month >> 3
DATA3 >> year >> 2020

ive got an outer FOREACH - (more data to the left of 'data1' / 'data2' etc..) and im left with the above arrays - but I only have ONE array at a time with just day, month & year)

is there a way to get the day value - without doing a separate foreach to find the day/month year - REGARDLESS of 'DATA1' / 'DATA2' / 'DATA3 etc ?

Or will i need another foreach on this sub-array ?
Back to top
View user's profile Send private message
gordonisnz
Smarty Regular


Joined: 05 Sep 2009
Posts: 69

PostPosted: Sat May 08, 2021 8:07 am    Post subject: Reply with quote

Ive found another thread to put "0" - but nothing seems to be working,.

$array.0.DAY
$array[0]["DAY"]
$array[0].DAY
Back to top
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Tue May 11, 2021 8:13 pm    Post subject: Reply with quote

My experiment:
Code:

{$Class|@print_r}
{$Class|array_column:3|@print_r}
Code:
Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
        )

    [1] => Array
        (
            [0] => E
            [1] => F
            [2] => G
            [3] => H
        )

    [2] => Array
        (
            [0] => I
            [1] => J
            [2] => K
            [3] => L
        )

    [3] => Array
        (
            [0] => M
            [1] => N
            [2] => O
            [3] => P
        )

)
1 << Residue from print_r
Array
(
    [0] => D
    [1] => H
    [2] => L
    [3] => P
)
1 << Residue from print_r

You could use:
{$Class|array_column:'day'}

(I note your array has 'day' but you are using DAY - using the wrong lettercase.)
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1745

PostPosted: Sat May 15, 2021 11:37 am    Post subject: Reply with quote

Code:
<pre>{$variable|print_r:true}</pre>
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