PHP code example of arokettu / json

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

    

arokettu / json example snippets




$obj = \Arokettu\Json\Json::decode('{"abc": 123}');

// we can access any data array-style
var_dump($obj['abc']);
// or object-style
var_dump($obj->abc);

// object will not turn into array
echo \Arokettu\Json\Json::encode($obj);



use Arokettu\Json\EncodeOptions;

// set options with methods
$options = EncodeOptions::build()
    ->withThrowOnError()
    ->withHexAmp();
// set options with PHP 8 named params
$options = EncodeOptions::build(
    throw_on_error: true,   // apply JSON_THROW_ON_ERROR 
    hex_amp: true,          // apply JSON_HEX_AMP 
);
// use both with this library and with the base function
$value = \Arokettu\Json\Json::encode($json, $options);
$value = json_encode($json, $options->value()); 
// pretty print existing options mix
echo EncodeOptions::build(4194752)->toString();
// will get you 'JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT'