PHP code example of abdelhamiderrahmouni / toon-php
1. Go to this page and download the library: Download abdelhamiderrahmouni/toon-php 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/ */
abdelhamiderrahmouni / toon-php example snippets
declare(strict_types=1);
use function Toon\encode;
$input = [
'name' => 'A',
'age' => 30,
'tags' => ['x', 'y', 'z'],
];
echo encode($input);
declare(strict_types=1);
use function Toon\encode;
$object = (object) [
'name' => 'A',
'age' => 30,
'tags' => ['x', 'y'],
];
echo encode($object);
declare(strict_types=1);
use function Toon\encode;
use Toon\Types\EncodeOptions;
$opts = new EncodeOptions(
indent = 4, // default 2
delimiter = "\t", // default ","
lengthMarker = '#', // default false
);
echo encode(['tags' => [1, 2, 3]], $opts);
declare(strict_types=1);
use function Toon\encode;
$json = '{"name":"A","age":30,"tags":["x","y","z"]}';
// Safest: throw on JSON errors and decode to associative arrays
try {
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
echo encode($data);
} catch (JsonException $e) {
// Handle invalid JSON
echo "Invalid JSON: " . $e->getMessage();
}
use function Toon\encode;
$contents = file_get_contents('data.json');
if ($contents === false) {
throw new RuntimeException('Failed to read data.json');
}
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
echo encode($data);
encode(['user' => ['name' => 'A', 'age' => 30]]);
encode(['tags' => ['x', 'y', 'z']]);
encode(['rows' => [[1, 2], [3]]]);
encode([
'rows' => [
['id' => 1, 'name' => 'a'],
['name' => 'b', 'id' => 2],
],
]);
encode([
'rows' => [
['id' => 1, 'name' => 'a'],
['id' => 2], // different shape
],
]);
encode([
'items' => [1, ['a', 'b'], ['x' => 1], 'z'],
]);
use Toon\Types\EncodeOptions;
$opts = new EncodeOptions(
indent = 2, // default: 2
delimiter = ",", // default: ","
lengthMarker = '#', // default: false
);
bash
composer