Re: [SMARTY] Section and Associative Arrays
| From: | sweatjejm at mchsi dot com | Date: | Fri, 06 Dec 2002 18:57:41 +0000 |
| Subject: | Re: [SMARTY] Section and Associative Arrays | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-6677@lists.php.net to get a copy of this message | ||
Malcolm,
Take a look at the foreach function in smarty.
http://smarty.php.net/manual/en/language.function.foreach.html
This should accomplish what you are looking for.
Jason
> Is it possible to get a section to iterate over an associative array?
>
> I've got one deeply multi-dimensional associative array which
> describes all the data I want Smarty to render into the template. As a
> new Smarty user, I assumed this would be a common/key use for Smarty.
>
> I can manage this:
>
> Array
> (
> [0] => Array
> (
> [objectid] => 9
> [objectname] => FOO
> [objectvalue] => Wibble
> )
> [1] => Array
> (
> [objectid] => 17
> [objectname] => BAR
> [objectvalue] => Wobble
> )
>
> )
>
> i.e. $var[0]['objectname'] = "FOO"
>
> This code:
>
> {section loop=$var name=item}
> Value: {$var[item]['objectvalue']}
> {/section}
>
> Prints:
> Value: Wibble
> Value: Wobble
>
> But what if I change the structure of the array as follows?
>
> Array
> (
> [FOO] => Array
> (
> [objectid] => 9
> [objectvalue] => Wibble
> )
> [BAR] => Array
> (
> [objectid] => 17
> [objectvalue] => Wobble
> )
>
> )
>
> The same Smarty code doesn't seem to produce a result for me. Is it
> possible to get a result? Similarly, if the key to the array is
> numeric but associative, as follows. Do I need to tell the section
> that it's an associative array? How?
>
> Array
> (
> [9] => Array
> (
> [objectname] => FOO
> [objectvalue] => Wibble
> )
> [17] => Array
> (
> [objectname] => BAR
> [objectvalue] => Wobble
> )
>
> )
>
> Are either of these cases possible? Am I doing something wrong?
> OK, some arrays could be restructured, but the associative arrays are
> a much more elegant way of describing the page data (print_r($var)
> looks almost like the raw XML rendering of the page). And I've got a
> sneaky feeling I'm going to run into more serious problems later if I
> can't use associative arrays.
>
> Regards,
>
> Malcolm.
>
> PS The manual doesn't show the raw arrays it's using for the examples,
> which means you have to infer the raw data from the output of the
> commands the examples are trying to teach you. It'd be a lot easier to
> read if this was changed.
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>