Write Data to YAML File using PHP

symfony/yaml library

  1. Add symfony/yaml library to composer.json file:
"require": {
    "symfony/yaml": "^5.0"
}
  1. Install library from the command line:
composer install
  1. Write data to YAML file:
<?php

use Symfony\Component\Yaml\Yaml;

require_once __DIR__ . '/vendor/autoload.php';

$data = [
    'parameters' => [
        'host' => 'localhost',
        'port' => 8080,
        'user' => 'root',
    ],
];

$yaml = Yaml::dump($data, indent: 2);
file_put_contents('test.yaml', $yaml);

Leave a Comment

Cancel reply

Your email address will not be published.