PHP code example of sunaoka / ndjson

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

    

sunaoka / ndjson example snippets


use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');

while ($json = $ndjson->readline()) {
    var_dump($json);
}

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');

foreach ($ndjson->readlines(3) as $jsons) {
    var_dump($jsons);
}

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');
$ndjson->writeline(['test' => '001']);
$ndjson->writeline(['test' => '002']);

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');
$ndjson->writelines([['test' => '001'], ['test' => '002']]);

array(1) {
  ["test"]=>
  string(3) "001"
}
array(1) {
  ["test"]=>
  string(3) "002"
}
array(1) {
  ["test"]=>
  string(3) "003"
}
array(1) {
  ["test"]=>
  string(3) "004"
}
array(1) {
  ["test"]=>
  string(3) "005"
}

array(3) {
  [0]=>
  array(1) {
    ["test"]=>
    string(3) "001"
  }
  [1]=>
  array(1) {
    ["test"]=>
    string(3) "002"
  }
  [2]=>
  array(1) {
    ["test"]=>
    string(3) "003"
  }
}
array(2) {
  [0]=>
  array(1) {
    ["test"]=>
    string(3) "004"
  }
  [1]=>
  array(1) {
    ["test"]=>
    string(3) "005"
  }
}