PHP code example of lnear / json

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

    

lnear / json example snippets


use Lame\JSONFile;

// Instantiate JSONFile with a file path
$jsonFile = new JSONFile('/path/to/file.json');

// Access and manipulate JSON data
$jsonFile->put('key', 'value');
$jsonFile->save(); // Save changes to the file

// Load JSON data from the file
$data = $jsonFile->load();

use Lame\JSONString;

// Instantiate JSONString with a JSON string
$jsonString = new JSONString('{"key": "value"}');

// Access and manipulate JSON data
$jsonString->put('newKey', 'newValue');
$jsonString->save(); // Save changes to the JSON string

// Load JSON data from the string
$data = $jsonString->load();

if ($jsonObject->has('name')) {
    echo $jsonObject['name']; // Outputs: John
}

$jsonObject['email'] = '[email protected]';
unset($jsonObject['age']);
echo count($jsonObject); // Outputs the count of items in the JSON object

$jsonObject->push('tags', 'developer');
$jsonObject->prepend('tags', 'programmer');
$jsonObject->increment('age', 2);
$jsonObject->decrement('age', 1);

$allData = $jsonObject->all();
$filteredData = $jsonObject->filter(fn($key, $value) => is_string($value));

$keysStartingWithA = $jsonObject->startingWith('a');
$jsonObject->forget('location');
$jsonObject->flush();
$jsonObject->flushStartingWith('temp_');

$jsonObject->each(fn($key, $value) => print("Key: $key, Value: $value"));
$mappedData = $jsonObject->map(fn($value) => strtoupper($value));
$reducedData = $jsonObject->reduce(fn($carry, $value) => $carry . $value, '');
$sortedData = $jsonObject->sort(fn($a, $b) => $a <=> $b);
$reversedData = $jsonObject->reverse();