简体   繁体   中英

Mustache (PHP) Outputting associative array keys

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

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.

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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