How can I read a YAML file with PHP? What I want is something like a reader.
Me and my friends are creating a Minecraft server and we want connect the server to site. For that we need a reader to read the YAML file that is in our server files. This file is called users.yml and inside it is the next code:
users:
80679a11-1d47-3a0e-8346-4790ee4304fc: <<<< Player Code.
group:
- Admin <<<< Player Group.
options:
name: JamesMarch <<<< Player Nick Name.
56874a35-8f52-5f2c-7843-7788je9670tb: <<<< Player Code.
group:
- Admin <<<< Player Group.
options:
name: Angelow98 <<<< Player Nick Name.
55026444-cb34-3a27-a270-d7d07fccca0a: <<<< Player Code.
group:
- Helper <<<< Player Group.
options:
name: iDatSnoow_ <<<< Player Nick Name.
When a new player connects to the server to play, this file automatically creates a new paragraph like this (This is a new player):
84569a84-5d77-3a5e-8547-4720ee4304fc: <<<< Player Code.
group:
- NewPlayer <<<< Player Group (NewPlayer is the default group)
options:
name: mumiant_ <<<< Player Nick Name.
To make the player an administrator, for example, one of the administrators, has to enter this command: /manuadd (player name) Admin
, and it will automatically edit in users.yml
.
In our website, on the home page, we would like to show, in a box, the most important groups (helpers and administrators) like this simple HTML code:
<h1>Staff</h1>
<h2>Administrators</h2>
<p>Angelow98</p>
<p>JamesMarch</p>
<h2>Helper</h2>
<p>iDatSnoow_</p>
In conclusion, what we want is that it work automatically. This is, when someone becomes an administrator, the PHP reader will read the YAML code and it will print on website, like the HTML code above.
You can parse YAML and dump an array to YAML using symfony/yaml:
use Symfony\Component\Yaml\Yaml;
$yaml = Yaml::parse(file_get_contents('/path/to/file.yml'));
$yamlString = Yaml::dump($yaml);
Now to parse your example, I replaced the <<<<
with valid YAML comments:
$data = \Symfony\Component\Yaml\Yaml::parse('users:
80679a11-1d47-3a0e-8346-4790ee4304fc: # Player Code.
group:
- Admin # Player Group.
options:
name: JamesMarch # Player Nick Name.
56874a35-8f52-5f2c-7843-7788je9670tb: # Player Code.
group:
- Admin # Player Group.
options:
name: Angelow98 # Player Nick Name.
55026444-cb34-3a27-a270-d7d07fccca0a: # Player Code.
group:
- Helper # Player Group.
options:
name: iDatSnoow_ # Player Nick Name.');
Now let's group all players by their first assigned group:
$groups = array();
foreach ($data['users'] as $playerCode => $player) {
$firstGroupName = $player['group'][0];
$groups[$firstGroupName][$playerCode] = $player;
}
$groups
now looks like this:
Array
(
[Admin] => Array
(
[80679a11-1d47-3a0e-8346-4790ee4304fc] => Array
(
[group] => Array
(
[0] => Admin
)
[options] => Array
(
[name] => JamesMarch
)
)
[56874a35-8f52-5f2c-7843-7788je9670tb] => Array
(
[group] => Array
(
[0] => Admin
)
[options] => Array
(
[name] => Angelow98
)
)
)
[Helper] => Array
(
[55026444-cb34-3a27-a270-d7d07fccca0a] => Array
(
[group] => Array
(
[0] => Helper
)
[options] => Array
(
[name] => iDatSnoow_
)
)
)
)
If you pass that array to a PHP template, you could achieve your output like this:
<?php foreach($groups as $group => $players): ?>
<h1><?= $group ?></h1>
<?php foreach ($players as $playerCode => $player): ?>
<p><?= $player['options']['name'] ?> </p>
<?php endforeach; ?>
<?php endforeach; ?>