How to convert a yaml to PHP array?

3

How could I do to convert yaml data to a array of PHP?

Example yaml:

Usuario:
    nome: Wallace
    idade: 25
    linguagens: 
        - PHP
        - Python
        - Javascript
    
asked by anonymous 28.08.2015 / 18:14

2 answers

4

In the PHP Manual , you have an example:

$yaml = <<<EOD
Usuario:
    nome: Wallace
    idade: 25
    linguagens: 
        - PHP
        - Python
        - Javascript
EOD;

  $parsed = yaml_parse($yaml);

 echo "<pre>";
  print_r($parsed);

Just be sure to enable the extension: extension=yaml.so on php.ini

To install: link
For linux: link

    
28.08.2015 / 18:41
5

A good way would be to use Yaml Component of Symphony .

See:

use Symfony\Component\Yaml\Parser;

$yaml = new Parser();

$value = $yaml->parse(file_get_contents('/arquivo.yml'));

Reference: The Yaml Component

    
28.08.2015 / 18:26