YAML parse return object not Array using PHP
According to documentation you should just pass Yaml::PARSE_OBJECT_FOR_MAP
as a second parameter of Yaml::parse()
:
$yaml = Yaml::parse(file_get_contents('test.yml'), Yaml::PARSE_OBJECT_FOR_MAP);
Symfony parsing yaml files from subfolders
Thanks Matteo for helpful answer, it didn't really fit into my needs, but helped.
I found following solution:
$yaml = new Parser();
$finder = new Finder();
$parsedData = array();
$tmp = $finder->files()->in(__DIR__.'/../path/to/folder')->name('test.yml');
foreach ($tmp as $t)
{
$x = $yaml->parse(file_get_contents($t));
array_push($parsedData, $x);
}
Reading Yaml with PHP
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; ?>
Parsing YAML file with PHP does not dump manipulated array back into correct YAML format
When you modify your array, you're removing elements without reindexing. Then yaml_emit
gets an array with a missing key, so it will try to make that weird syntax. I'm not aware if this is a bug on the library or the desired behavior.
To get the output you want, change these line (2 occurrences):
// Before
$landAfterDump = yaml_emit($array);
// After
$landAfterDump = yaml_emit(array_values($array));
Related Topics
Set Active State on Navigation Dynamically
Sent Mails with PHPmailer Don't Go to "Sent" Imap Folder
Using PHP as a Template Engine
Sum Specific Values in a Multidimensional Array (Php)
Resetting MySQL Root Password with Xampp on Localhost
Laravel Migration Table Already Exists, But I Want to Add New Not the Older
How to Set a Class Attribute to a Symfony2 Form Input
Send Xml Data to Webservice Using PHP Curl
Htmlspecialchars VS HTMLentities When Concerned with Xss
Dynamically Changing Image Colours
PHP SQL, Query Returns Only One Row of Data
Compile Error: Cannot Use Isset() on the Result of an Expression
Comma-Separated String to Array
How to Solve Error 404 When Deploying Laravel Project to Cpanel