PHP 在同一 YAML 文件中定位特定的 YAML 文档

问题描述 投票:0回答:1

在 PHP 中,使用 YAML 时,是否可以使用 yaml 文件内的特定文档?

例如

--- #settings
Settings go here
...

--- #user
Users go here
...

是否可以仅将用户加载到 php 中,以便我只能使用该文档数据?

php yaml
1个回答
0
投票

使用 Symfony YAML 组件或 Spyc

<?php
require_once 'vendor/autoload.php'; // Load the autoload from Symfony YAML Component

use Symfony\Component\Yaml\Yaml;

// YAML file path
$file = 'path/to/your/file.yaml';

// Load the entire YAML file
$data = Yaml::parseFile($file);

// Access the data for the 'user' section
$usersData = $data['user'];

// Now you can work with $usersData
// ...
?>
© www.soinside.com 2019 - 2024. All rights reserved.