PHP code example of inkrot / php-compress-json

1. Go to this page and download the library: Download inkrot/php-compress-json 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/ */

    

inkrot / php-compress-json example snippets


use CompressJson\Core\Compressor;

$data = [
    'infinity' => INF,
    'negInfinity' => -INF,
    'nan' => NAN,
];

$compressedJson = Compressor::create()
    ->compress($data)
    ->toJson();

// Decompress
$decompressed = Compressor::create()
    ->decompressJson($compressedJson);

var_dump($decompressed['infinity']);    // float(INF)
var_dump($decompressed['negInfinity']); // float(-INF)
var_dump(is_nan($decompressed['nan'])); // bool(true)

use CompressJson\Core\Compressor;

$data = [
    'key1' => 'value1',
    'key2' => 'value3',
    'key3' => [
        'nestedKey1' => 'value1'
    ]
];
$compressedJson = Compressor::create()
    ->compress($data)
    ->toJson();

print_r($compressedJson); // encoded string in JSON format

use CompressJson\Core\Compressor;

$compressedJson = '[["key1","key2","key3","a|0|1|2","value1","value3","nestedKey1","a|6","o|7|4","o|3|4|5|8"],"9"]';
$data = Compressor::create()
    ->decompressJson($compressedJson);

print_r($data); // array

use CompressJson\Core\Compressor;
$data = [
    'count' => 3,
    'names' => ['New York', 'London', 'Paris', 'Beijing', 'Moscow'],
    'cities' => [
        [
            'id' => 1,
            'name' => 'New York',
            'countryName' => 'USA',
            'location' => [
                'latitude' => 40.714606,
                'longitude' => -74.002800,
            ],
            'localityType' => 'BIG_CITY',
        ],
        [
            'id' => 2,
            'name' => 'London',
            'countryName' => 'UK',
            'location' => [
                'latitude' => 51.507351,
                'longitude' => -0.127696,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
        [
            'id' => 3,
            'name' => 'Paris',
            'countryName' => 'France',
            'location' => [
                'latitude' => 48.856663,
                'longitude' => 2.351556,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
        [
            'id' => 4,
            'name' => 'Beijing',
            'countryName' => 'China',
            'location' => [
                'latitude' => 39.901850,
                'longitude' => 116.391441,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
        [
            'id' => 5,
            'name' => 'Moscow',
            'countryName' => 'Russia',
            'location' => [
                'latitude' => 55.755864,
                'longitude' => 37.617698,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
    ]
];
$compressedJson = Compressor::create()
    ->compress($data)
    ->toJson();
    
print_r(json_encode($data));
echo PHP_EOL;
print_r($compressedJson);
 bash
composer