Array keys, values

View: New views
9 Messages — Rating Filter:   Alert me  

Array keys, values

by Christoph Boget-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If I have an array that looks like this:

Array
(
    [Salary] => Array
        (
            [0-50K] => Array
                (
                    [filterType] => range
                    [fieldLabel] => 0-50K
                    [fieldName] => SALARY
                    [lowerValue] => 0
                    [upperValue] => 50
                    [inclusive] => LEFT
                )

            [50-70K] => Array
                (
                    [filterType] => range
                    [fieldLabel] => 50-70K
                    [fieldName] => SALARY
                    [lowerValue] => 50
                    [upperValue] => 70
                    [inclusive] => BOTH
                )
        )
)


How can I access the key values?  Individually, that is.  What about the
child array key/values?  Individually, that is.  In the template, I've
tried:

{$myArray[0]}
{$myArray[0].filterType}
{$myArray[0][0]}

Nothing seems to work.  I know how to iterate through the entire array and
display the contents that way, but I don't know how to get the key/value of
just one particular element (and/or any child arrays of that element).

Any help would be greatly appreciated!

thnx,
Christoph

RE: Array keys, values

by Max Schwanekamp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Christoph Boget wrote:
>If I have an array that looks like this:
>Array ([Salary] => Array ( [0-50K] => Array ( [filterType] => range ) ) )
>
>How can I access the key values?  Individually, that is.  What about the
>child array key/values?  Individually, that is.

{$myArray.Salary.0-50K.filterType} should work, but the key 0-50K might be
problematic due to the leading numeric character (I haven't tried it).  If
it doesn't work, try adding a leading alpha char to those key names.

--
Max Schwanekamp
NeptuneWebworks.com
541-517-9064





How can I access the key values?  Individually, that is.  What about the
child array key/values?  Individually, that is.  In the template, I've
tried:

{$myArray[0]}
{$myArray[0].filterType}
{$myArray[0][0]}

Nothing seems to work.  I know how to iterate through the entire array and
display the contents that way, but I don't know how to get the key/value of
just one particular element (and/or any child arrays of that element).

Any help would be greatly appreciated!

thnx,
Christoph

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Array keys, values

by Christoph Boget-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10/15/07, Max Schwanekamp <lists@...> wrote:

>
> Christoph Boget wrote:
> >If I have an array that looks like this:
> >Array ([Salary] => Array ( [0-50K] => Array ( [filterType] => range ) ) )
> >
> >How can I access the key values?  Individually, that is.  What about the
> >child array key/values?  Individually, that is.
>
> {$myArray.Salary.0-50K.filterType} should work, but the key 0-50K might be
> problematic due to the leading numeric character (I haven't tried it).  If
> it doesn't work, try adding a leading alpha char to those key names.


And if I don't know the key value off hand because the array was generated
dynamically?

As an aside, 2 features that would be really nice when working with arrays
in a smarty template:

* get the total count of the elements w/o having to iterate through the
array
* be able to access the first or last element even if the keys aren't
numeric

thnx,
Christoph

Re: Array keys, values

by Monte Ohrt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can get the count of an array with {$array|@count}

But, you are better off assigning the count if you need it, and also
assigning your keys/index values that make things as simple as possible
in the template. Instead of assuming the template should be able to
traverse your data in any format, instead format your data in a way that
is a easy as possible to traverse and display.

Christoph Boget wrote:

> On 10/15/07, Max Schwanekamp <lists@...> wrote:
>  
>> Christoph Boget wrote:
>>    
>>> If I have an array that looks like this:
>>> Array ([Salary] => Array ( [0-50K] => Array ( [filterType] => range ) ) )
>>>
>>> How can I access the key values?  Individually, that is.  What about the
>>> child array key/values?  Individually, that is.
>>>      
>> {$myArray.Salary.0-50K.filterType} should work, but the key 0-50K might be
>> problematic due to the leading numeric character (I haven't tried it).  If
>> it doesn't work, try adding a leading alpha char to those key names.
>>    
>
>
> And if I don't know the key value off hand because the array was generated
> dynamically?
>
> As an aside, 2 features that would be really nice when working with arrays
> in a smarty template:
>
> * get the total count of the elements w/o having to iterate through the
> array
> * be able to access the first or last element even if the keys aren't
> numeric
>
> thnx,
> Christoph
>
>  

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Array keys, values

by Josh Trutwin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> * get the total count of the elements w/o having to iterate through
> the array

{$my_array|@count}

> * be able to access the first or last element even if the keys
> aren't numeric

Not very efficient but could do:

{foreach from=$my_array item=arr name=arr_loop}
   {if $smarty.foreach.arr_loop.first}
      do something for first only
   {/if}
{/foreach}

Josh

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Array keys, values

by messju mohr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Oct 15, 2007 at 04:31:23PM -0500, Josh Trutwin wrote:
> > * get the total count of the elements w/o having to iterate through
> > the array
>
> {$my_array|@count}
>
> > * be able to access the first or last element even if the keys
> > aren't numeric

{$my_array|@reset}

... just my 2 ct


> Not very efficient but could do:
>
> {foreach from=$my_array item=arr name=arr_loop}
>    {if $smarty.foreach.arr_loop.first}
>       do something for first only
>    {/if}
> {/foreach}
>
> Josh

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Array keys, values

by messju mohr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Oct 16, 2007 at 09:03:30AM +0200, messju mohr wrote:

> On Mon, Oct 15, 2007 at 04:31:23PM -0500, Josh Trutwin wrote:
> > > * get the total count of the elements w/o having to iterate through
> > > the array
> >
> > {$my_array|@count}
> >
> > > * be able to access the first or last element even if the keys
> > > aren't numeric
>
> {$my_array|@reset}

and (I forgot) {$my_array|@end} to access the last element. these two
expressions just use http://php.net/reset and http://php.net/end as
modifiers.

> ... just my 2 ct
>
>
> > Not very efficient but could do:
> >
> > {foreach from=$my_array item=arr name=arr_loop}
> >    {if $smarty.foreach.arr_loop.first}
> >       do something for first only
> >    {/if}
> > {/foreach}
> >
> > Josh
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Array keys, values

by Christoph Boget-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
> You can get the count of an array with {$array|@count}


Where can I find out about this in the documentation?  I don't see a
section  for something like this.  And when I did a search  for '@count' in
the "english manual, the only thing that was returned was a page for
{html_options} and the only reason that was returned is because @count was
used in a comment on that page.

But, you are better off assigning the count if you need it, and also


But why would you assign it if there is another way to get it?

thnx,
Christoph

Re: Array keys, values

by Monte Ohrt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It's in the modifiers section of the manual.

Christoph Boget wrote:

>> You can get the count of an array with {$array|@count}
>>    
>
>
> Where can I find out about this in the documentation?  I don't see a
> section  for something like this.  And when I did a search  for '@count' in
> the "english manual, the only thing that was returned was a page for
> {html_options} and the only reason that was returned is because @count was
> used in a comment on that page.
>
> But, you are better off assigning the count if you need it, and also
>
>
> But why would you assign it if there is another way to get it?
>
> thnx,
> Christoph
>
>  

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

LightInTheBox - Buy quality products at wholesale price