简体   繁体   English

Mustache (PHP) 输出关联数组键

[英]Mustache (PHP) Outputting associative array keys

In Mustache can I print out the name of an associative array key instead of its value?在 Mustache 中,我可以打印出关联数组键的名称而不是其值吗?

ie 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}}
    {{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.我确信 OP 已经继续前进,但是对于任何偶然发现这篇文章的人,我只想指出,这是不可能的原因是因为没有可预测的方法来引用该数组中的任何内容。

Think of a key in terms of a map, and you have more elaboration.想一想 map 方面的键,你有更多的细节。

Use array_keys() .使用array_keys() Or if you want to reverse index => value to value => index you can use array_flip() .或者,如果您想将 index => value 反转为 value => index,您可以使用array_flip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM