S
S
symnoob2021-03-07 22:34:33
symfony
symnoob, 2021-03-07 22:34:33

Symfony - how to read yaml File and get certain data?

Hello everyone, how can I read data from a yaml file?

according to the documentation, it works like this:

https://symfony.com/doc/current/components/yaml.html#reading-yaml-files

use Symfony\Component\Yaml\Yaml;

$value = Yaml::parseFile('/path/to/file.yaml');


but how can you get only certain data? is it really necessary to run through the array and collect them manually?

profileData:
   Fields:
      - { property: 'name', label: 'name', type: 'string' }
      - { property: 'firstname', label: 'firstname', type: 'string' }
      - { property: 'street', label: 'street' }


I need to get only label and type, while type is not found everywhere.
is there any way to get this data out of yaml?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netrox, 2021-03-08
@symnoob

$yaml = Yaml::parse('/path/to/file.yaml');    

$data = array_map(function ($field) {
  $item = [];
  if (isset($field['label'])) {
    $item['label'] = $field['label'];
  }
  if (isset($field['type'])) {
     $item['type'] = $field['type'];
  }
  return $item;
}, $yaml['profileData']['Fields']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question