Smarty: Correct Array Missing Keys
I'm calling an API via PHP that returns a multi-dimensional array [$myArray].
I want to pass this array for use in my Smarty template but for some reason the array is returning without the first dimension keys.
When I print_r on $myArray in PHP I get:
Array (
[success] => 1
[errors] => 0
[data] => Array (
[0] => Array (
[email] => myemail@email.com
[id] => hhd77sjr
[timestamp] => 2011-08-10 16:31:29
)
)
)
I assign this array as:
$smarty->assign('my_array',$myArray);
Then when I access my_array in my Smarty template via {$my_array|print_r}
I get:
1
0
Array (
[0] => Array (
[email] => myemail@email.com
[id] => hhd77sjr
[timestamp] => 2011-08-10 16:31:29
)
)
What happened to my keys for the [success], [errors] and [data] values? I'm only seeing the values. How 开发者_Go百科can I access those values via something like: {$my_array.success}
?
THANKS for your help!!
If you had been looked up on google searching for "smarty array", you could found this:
<ul>
{foreach from=$myArray key=k item=v}
<li>{$k}: {$v}</li>
{/foreach}
</ul>
http://www.smarty.net/docsv2/en/language.function.foreach
Without looking into the source, I'm guessing it has to do with how Smarty displays the result of print_r()
. By default print_r tries to output directly, but looking at the PHP documentation:
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.
Source
You certainly should be able to access all the values normally, ie, {$my_array.success}
.
If you'd like to try print_r
again, try this: {$my_array|print_r:true}
(set the return parameter to true).
精彩评论