开发者

Mustache (PHP) Outputting associative array keys

In Mustache can I print out the name of an associative array key instead of its value?

i.e. So instead of this:

$cars= array(
  'name'=>'ferrari', 'color'=>'red', 
  'name'=>'lambo', 'color'=>'yellow'
);
....
{{#cars}}
    {{name}} is {{color}}
{{/cars}}

I would prefer to have a data source with a smaller footprint:

$cars= array('ferrari'=>'red', 'lambo'=>'yellow');
....
{{#cars}}
  开发者_Python百科  {{array_key_here}} is {{.}}
{{/cars}}

Is it possible?


I'm sure the OP has already moved on, but to anyone stumbling upon this post, I'd just like to point out that the reason this is not possible is because there is no predictable means of referencing anything in that array.

Think of a key in terms of a map, and you have more elaboration.


Use array_keys(). Or if you want to reverse index => value to value => index you can use array_flip().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜