YAML and PHP: Examples of arrays and anchors

Wooden anchor with a string on a piece of cloth.
Example of an anchor.

I recently had to get my feet wet with YAML, so I’ll briefly explain how to get it up and running on your server, show some examples of how to represent multidimensional associative and indexed arrays, and lastly, how to utilize anchors, which is my favorite feature so far.

Getting YAML setup with PHP

If you’re running CentOS or RedHat, you can download the YAML PHP package with:

yum install php-pecl-yaml

Then restart your web server, Apache in my case, using something like:

service httpd reload

That’s it! Now you have YAML ready to go.

Parsing YAML with PHP

Now that you’ve downloaded the YAML PHP package, you have a new function to parse YAML files. Simply provide it with the path to your YAML file and you’ll get an array in return:

$yaml_array = yaml_parse_file('/var/www/domains/.../templates.yaml');

Indenting YAML

When indenting lines, use spaces, not tabs. If you use a tab, you’ll get an error like this:

Warning: yaml_parse_file(): scanning error encountered during parsing: found character that cannot start any token (line 3, column 1), context while scanning for the next token (line 3, column 1) in ...

Array samples of YAML input and PHP output

In the following examples, a dash will create an array element with a numeric key whereas a string followed by a colon will create an array element with a user-defined key.

Indexed array

YAML:

- Template 1
- Template 2
- Template 3

PHP:

Array
(
  [0] => Template 1
  [1] => Template 2
  [2] => Template 3
)

Associative array

YAML:

template1: Template 1
template2: Template 2
template3: Template 3

PHP:

Array
(
  [template1] => Template 1
  [template2] => Template 2
  [template3] => Template 3
)

Indexed array of indexed arrays

YAML:

-
  - Template 1
  - template-1.html.twig
-
  - Template 2
  - template-2.html.twig
-
  - Template 3
  - template-3.html.twig

PHP:

Array
(
  [0] => Array
    (
      [0] => Template 1
      [1] => template-1.html.twig
    )

  [1] => Array
    (
      [0] => Template 2
      [1] => template-2.html.twig
    )

  [2] => Array
    (
      [0] => Template 3
      [1] => template-3.html.twig
    )
)

Indexed array of associative arrays

YAML:

-
  name: Template 1
  file: template-1.html.twig
-
  name: Template 2
  file: template-2.html.twig
-
  name: Template 3
  file: template-3.html.twig

PHP:

Array
(
  [0] => Array
    (
      [name] => Template 1
      [file] => template-1.html.twig
    )

  [1] => Array
    (
      [name] => Template 2
      [file] => template-2.html.twig
    )

  [2] => Array
    (
      [name] => Template 3
      [file] => template-3.html.twig
    )
)

Associative array of indexed arrays

YAML:

template1:
  - Template 1
  - template-1.html.twig
template2:
  - Template 1
  - template-1.html.twig
template3:
  - Template 1
  - template-1.html.twig

PHP:

Array
(
  [template1] => Array
    (
      [0] => Template 1
      [1] => template-1.html.twig
    )

  [template2] => Array
    (
      [0] => Template 1
      [1] => template-1.html.twig
    )

  [template3] => Array
    (
      [0] => Template 1
      [1] => template-1.html.twig
    )
)

Associative array of associative arrays

YAML:

template1:
  name: Template 1
  file: template-1.html.twig
template2:
  name: Template 1
  file: template-1.html.twig
template3:
  name: Template 1
  file: template-1.html.twig

PHP:

Array
(
  [template1] => Array
    (
      [name] => Template 1
      [file] => template-1.html.twig
    )

  [template2] => Array
    (
      [name] => Template 1
      [file] => template-1.html.twig
    )

[template3] => Array
  (
      [name] => Template 1
      [file] => template-1.html.twig
    )
)

Anchor samples of YAML input and PHP output

The ampersand specifies the variable name and with the asterisk you can then use the variable. The variable will contain whatever value falls below it.

Linking values from indexed array to indexed array

YAML:

templates:
  - &t1
      name: Test Template 1
      file: test-template1.html.twig
  - &t2
      name: Test Template 2
      file: test-template2.html.twig
  - &t3
      name: Test Template 3
      file: test-template3.html.twig
pages:
  p1:
    - *t1
    - *t2
  p2:
    - *t2
    - *t3

PHP:

Array
(
    [templates] => Array
      (
        [0] => Array
          (
            [name] => Test Template 1
            [file] => test-template1.html.twig
          )

        [1] => Array
          (
            [name] => Test Template 2
            [file] => test-template2.html.twig
          )

        [2] => Array
          (
            [name] => Test Template 3
            [file] => test-template3.html.twig
          )
      )

    [pages] => Array
      (
        [p1] => Array
          (
            [0] => Array
              (
                [name] => Test Template 1
                [file] => test-template1.html.twig
              )

            [1] => Array
              (
                [name] => Test Template 2
                [file] => test-template2.html.twig
              )
          )

        [p2] => Array
          (
            [0] => Array
              (
                [name] => Test Template 2
                [file] => test-template2.html.twig
              )

            [1] => Array
              (
                [name] => Test Template 3
                [file] => test-template3.html.twig
              )
          )
      )
)

Linking values from indexed array to associative array

YAML:

templates:
  - &t1
      name: Test Template 1
      file: test-template1.html.twig
  - &t2
      name: Test Template 2
      file: test-template2.html.twig
  - &t3
      name: Test Template 3
      file: test-template3.html.twig
pages:
  p1:
    template1: *t1
    template2: *t2
  p2:
    template2: *t2
    template3: *t3

PHP:

Array
(
  [templates] => Array
    (
      [0] => Array
        (
          [name] => Test Template 1
          [file] => test-template1.html.twig
        )

      [1] => Array
        (
          [name] => Test Template 2
          [file] => test-template2.html.twig
        )

      [2] => Array
        (
          [name] => Test Template 3
          [file] => test-template3.html.twig
        )
    )

  [pages] => Array
    (
      [p1] => Array
        (
          [template1] => Array
            (
              [name] => Test Template 1
              [file] => test-template1.html.twig
            )

          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )
        )

      [p2] => Array
        (
          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )

          [template3] => Array
            (
              [name] => Test Template 3
              [file] => test-template3.html.twig
            )
        )
    )
)

Linking values from associative array to indexed array

YAML:

templates:
  template1: &t1
    name: Test Template 1
    file: test-template1.html.twig
  template2: &t2
    name: Test Template 2
    file: test-template2.html.twig
  template3: &t3
    name: Test Template 3
    file: test-template3.html.twig
pages:
  p1:
    - *t1
    - *t2
  p2:
    - *t2
    - *t3

PHP:

Array
(
  [templates] => Array
    (
      [template1] => Array
        (
          [name] => Test Template 1
          [file] => test-template1.html.twig
        )

      [template2] => Array
        (
          [name] => Test Template 2
          [file] => test-template2.html.twig
        )

      [template3] => Array
        (
          [name] => Test Template 3
          [file] => test-template3.html.twig
        )
    )

  [pages] => Array
    (
      [p1] => Array
        (
          [0] => Array
            (
              [name] => Test Template 1
              [file] => test-template1.html.twig
            )

          [1] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )
        )

      [p2] => Array
        (
          [0] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )

          [1] => Array
            (
              [name] => Test Template 3
              [file] => test-template3.html.twig
            )
        )
    )
)

Linking values from associative array to associative array

YAML:

templates:
  template1: &t1
    name: Test Template 1
    file: test-template1.html.twig
  template2: &t2
    name: Test Template 2
    file: test-template2.html.twig
  template3: &t3
    name: Test Template 3
    file: test-template3.html.twig
pages:
  p1:
    template1: *t1
    template2: *t2
  p2:
    template2: *t2
    template3: *t3

PHP:

Array
(
  [templates] => Array
    (
      [template1] => Array
        (
          [name] => Test Template 1
          [file] => test-template1.html.twig
        )

      [template2] => Array
        (
          [name] => Test Template 2
          [file] => test-template2.html.twig
        )

      [template3] => Array
        (
          [name] => Test Template 3
          [file] => test-template3.html.twig
        )
    )

  [pages] => Array
    (
      [p1] => Array
        (
          [template1] => Array
            (
              [name] => Test Template 1
              [file] => test-template1.html.twig
            )

          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )
        )

      [p2] => Array
        (
          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )

          [template3] => Array
            (
              [name] => Test Template 3
              [file] => test-template3.html.twig
            )
        )
    )
)

Linking arrays to arrays

The double less than sign basically injects the contents into the parent.

YAML:

templates:
  - &t1
    template1:
      name: Test Template 1
      file: test-template1.html.twig
  - &t2
    template2:
      name: Test Template 2
      file: test-template2.html.twig
  - &t3
    template3:
      name: Test Template 3
      file: test-template3.html.twig
pages:
  p1:
    <<: *t1
    <<: *t2
  p2:
    <<: *t1
    <<: *t2

PHP:

Array
(
  [templates] => Array
    (
      [0] => Array
        (
          [template1] => Array
            (
              [name] => Test Template 1
              [file] => test-template1.html.twig
            )
        )

      [1] => Array
        (
          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )
        )

      [2] => Array
        (
          [template3] => Array
            (
              [name] => Test Template 3
              [file] => test-template3.html.twig
            )
        )
    )

  [pages] => Array
    (
      [p1] => Array
        (
          [template1] => Array
            (
              [name] => Test Template 1
              [file] => test-template1.html.twig
            )

          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )
        )

      [p2] => Array
        (
          [template1] => Array
            (
              [name] => Test Template 1
              [file] => test-template1.html.twig
            )

          [template2] => Array
            (
              [name] => Test Template 2
              [file] => test-template2.html.twig
            )
        )
    )
)

Conclusion

It’s much easier to learn by example, so hopefully you saw something useful that you can adopt right away!

Featured image by W.S. Coda.


Comments (2)

Previously posted in WordPress and transferred to Ghost.

Chris
November 16, 2017 at 4:14 pm

Good article thank you. Are you aware of any way to convert a POST request coming in as MIME application\x-yaml to an array with this library?

Anti Matter
May 31, 2019 at 11:49 am

Very strange article. The php code shown is not valid php code, not even close, strings aren’t properly quoted, no commas, none of this works for me.