Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}}
    {{array_key_here}} is {{.}}
{{/cars}}

Is it possible?

like image 850
Globalz Avatar asked Jul 13 '11 01:07

Globalz


2 Answers

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.

like image 92
Mike Avatar answered Oct 17 '22 04:10

Mike


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

like image 30
Zaffy Avatar answered Oct 17 '22 04:10

Zaffy