PHP code example of indykoning / php-jsonl

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

    

indykoning / php-jsonl example snippets


\Indykoning\Jsonl\Jsonl::decode($jsonl, $associative);

\Indykoning\Jsonl\Jsonl::decodeFromResource($resource, $associative);

$data = \Indykoning\Jsonl\Jsonl::decodeFromResource(fopen('/tmp/data.jsonl', 'r'), true);

$data = \Indykoning\Jsonl\Jsonl::decodeFromResource(
    \Illuminate\Support\Facades\Http::get('https://example.com/data.jsonl')->resource()
);

\Indykoning\Jsonl\Jsonl::encode($array);

\Indykoning\Jsonl\Jsonl::encode([
    ['name' => 'Gilbert', 'session' => '2013', 'score' => 24, 'completed' => true],
    ['name' => 'Alexa', 'session' => '2013', 'score' => 29, 'completed' => true],
]);

\Indykoning\Jsonl\Jsonl::encodeToResource($resource, $array);

\Indykoning\Jsonl\Jsonl::encodeToResource(
    fopen('/tmp/data.jsonl', 'w'), 
    [
        ['name' => 'Gilbert', 'session' => '2013', 'score' => 24, 'completed' => true],
        ['name' => 'Alexa', 'session' => '2013', 'score' => 29, 'completed' => true],
    ]
);

function enrichData($data) 
{
    foreach($data as $object)
    {
        $object->enriched_data = EnrichmentModel::find($object->id)->toArray();

        yield $object;
    }
}

response()->streamDownload(
    function () {
        $data = \Indykoning\Jsonl\Jsonl::decodeFromResource(
            \Illuminate\Support\Facades\Http::get('https://example.com/data.jsonl')->resource()
        );

        $data = enrichData($data);

        foreach(\Indykoning\Jsonl\Jsonl::encode($data) as $chunk) {
            echo $chunk;
            ob_flush();
            flush();
        }
    },
    'data.jsonl',
    [
        'Content-Type' => 'application/jsonl',
        'X-Accel-Buffering' => 'no'
    ]
);
 bash
composer