How to get an array out of a Yaml file (using Symfony’s parser)

Working with Symfony is the first time I’ve worked with Yaml files. As such, I still have to look up the info when I’m trying to do something like get an array out of a mass of Yaml configurations settings. Searching around, for info on the subject, I found the following, helpful conversation.

ifilipov writes:

Hey guys, my navigation is located in app.yml

all:
  navigation:
    Home: index.php
    About us: about.php
    Events: events.php
    Mailing List: mlist.php
    Contact Us: feedback.php

i’m trying to put all this in array, but it doesn’t work.

$nav = sfConfig::get(’app_navigation’);
print_r($nav);

halfer replies:

I believe to collapse things down to an array, you can also use the dotted syntax, something like this:

all:
   .settings
     navigation:
       Home: index.php
       About us: about.php
       Events: events.php
       Mailing List: mlist.php
       Contact Us: feedback.php

The dotted word can be anything, and is ignored by the YAML parser. Then to access it:

$nav = sfConfig::get(’app_navigation’);
print_r($nav);

Now things get strange.

I thought it would be useful to double-check with the official Yaml spec, to see what the dot really means. But the dot is not in use in any of the examples given in the spec. Instead,

To do a 2 dimensional array, there is this:

Example 2.3. Mapping Scalars to Sequences
(ball clubs in each league)

american:
   – Boston Red Sox
   – Detroit Tigers
   – New York Yankees
national:
   – New York Mets
   – Chicago Cubs
   – Atlanta Braves

Here is something I take to be what in PHP would be considered an associative array inside of non-associative array:

product:
   – sku : BL394D
      quantity : 4
      description : Basketball
      price : 450.00
   – sku : BL4438H
      quantity : 1
      description : Super Hoop
      price : 2392.00

But I find no examples using the dot notation. Which is odd.

(As to the conversation on the forum, partly I link to these articles to give them some rank with Google. I find that Google does a poor job of figuring out what pages are relevant to Symfony searches. I think this is partly because Symfony bloggers don’t link to many tutorials or quick tips, so Google doesn’t have many links on which to form a judgement about what is important.)

Leave a Reply