PHP code example of exayer / vdf-converter
1. Go to this page and download the library: Download exayer/vdf-converter library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
exayer / vdf-converter example snippets
$vdf = '{
"mercury" {
"distance" "58"
}
"venus" {
"distance" "108"
}
"earth" {
"distance" "149"
}
}';
use EXayer\VdfConverter\VdfConverter;
// $vdf declaration
$planets = VdfConverter::fromString($vdf);
$planets = VdfConverter::fromFile('data://text/plain,' . $vdf); // usually filename here
$tempFile = tmpfile();
fwrite($tempFile, $vdf);
fseek($tempFile, 0);
$planets = VdfConverter::fromStream($tempFile);
$planets = VdfConverter::fromIterable([substr($vdf, 0, -60), substr($vdf, -60)]);
foreach ($planets as $name => $data) {
// #1 iteration: $name === "mercury" $data === ["distance" => "58"]
// #2 iteration: $name === "venus" $data === ["distance" => "108"]
// #3 iteration: $name === "earth" $data === ["distance" => "149"]
}
$result = iterator_to_array($planets);
//
// $result = [
// "mercury" => [
// "distance" => "58"
// ]
// "venus" => [
// "distance" => "108"
// ]
// "earth" => [
// "distance" => "149"
// ]
// ]
$vdf = '{
"mercury" {
"distance" "58"
"velocity" "35"
"distance" "92"
}
"mercury" {
"distance" "108"
}
"earth" {
"distance" "149"
}
}';
$result = iterator_to_array(VdfConverter::fromString($vdf));
//
// $result = [
// "mercury" => [
// "distance" => "58"
// "velocity" => "35"
// "distance__[2]" => "92"
// ]
// "mercury__[2]" => [
// "distance" => "108"
// ]
// "earth" => [
// "distance" => "149"
// ]
// ]
namespace EXayer\VdfConverter\UniqueKey;
interface Formatter
{
public function buildKeyName(string $key, int $index): string;
}
$config = VdfConverterConfig::create()
->uniqueKeyFormatter(YourCustomFormatter::class);
$iterator = VdfConverter::fromString($vdf, $config);