Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to get Key/value pair from Yaml file?


Go to End


3 Posts   821 Views

Avatar
mi32dogs

Community Member, 75 Posts

20 June 2016 at 9:30am

I hace a states list in my config.yml file like this

MyConfig:
  States:
    AL: Alabama
    AK: Alaska
    AZ: Arizona
    AR: Arkansas
    CA: California
    CO: Colorado

and with this i can populate dropdown like this Config::inst()->get('MyConfig', 'States'); this works great.

but now i just wand to get the Key/Value pairs so i can process them but if i do
$states = Config::inst()->get('MyConfig', 'States');
i'm only getting the values not the Keys how do i get them both?

Thank you

Avatar
martimiz

Forum Moderator, 1391 Posts

26 June 2016 at 11:27pm

Edited: 26/06/2016 11:30pm

For me it all seems to work just fine:

$array = Config::inst()->get('MyConfig', 'States');
foreach ($array as $key => $value) echo "$key: $value<br>";

outputs:

AL: Alabama
AK: Alaska
AZ: Arizona
AR: Arkansas
CA: California
CO: Colorado

And just in case:

echo $array['CA'];

outputs: California :)

Avatar
mi32dogs

Community Member, 75 Posts

27 June 2016 at 5:13pm

I’m so sorry, you are right it works, what it did what get a Debug::Show() of the array and it only gave me the value’s not the keys what made me think the keys where not there, but they are.

Thank you so much