PHP code example of adinan-cenci / json-lines

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

    

adinan-cenci / json-lines example snippets


use AdinanCenci\JsonLines\JsonLines;

$associative = true;
$file = new JsonLines('my-file.jsonl', $associative);

foreach ($file->objects as $line => $object) {
    echo $object->myProperty . '<br>';
    // or $object['myProperty'] if ::$associative is true.
}

$object = ['foo' => 'bar'];
$file->addObject($object);

$line = 5;
$object = ['foo' => 'bar'];
$file->addObject($object, $line);

$objects = [
// line => object
    0 => ['name' => 'foo'],
    5 => ['name' => 'bar'],
];

$objects->addObjects($objects);

$objects = [
// line => object / array
    2 => ['name' => 'foo'],
    6 => ['name' => 'bar'],
];

$objects->addObjects($objects, false);

$line   = 10;
$object = ['foo' => 'bar'];
$file->setObject($line, $object);

$objects = [
// line => object / array
    0 => ['name' => 'foo'],
    5 => ['name' => 'bar'],
];

$objects->setObjects($objects);

$line   = 10;
$object = $file->getObject($line);

$lines   = [0, 1, 2];
$objects = $file->getObjects($lines);

$line = 10;
$file->deleteObject($line);

$lines = [0, 1, 2];
$file->deleteObjects($lines);

$search = $file->search();
$search->condition("object's property", 'value to compare', 'operator');
$results = $search->find();

$search->condition('title', null, 'IS NULL');
// Will match entries where the "title" property equals null or is 
// not defined.

$search->condition('title', 'Iliad', '=');
// Will match entries where the "title" property equals "Iliad" 
// ( case insensitive ).

$search->condition('title', ['Iliad', ' Odyssey'], 'IN');
// Will match entries where the "title" property equals to either 
// "Iliad" or "Odyssey" ( case insensitive ).

$search->condition('title', 'foo', 'LIKE');
// Will match entries where the "title" property contains the word "foo"
// e.g: "foo", "foo bar", "foofighters" etc ( case insensitive ).

$search->condition('title', ['foo', 'bar'], 'LIKE');
// It also accept arrays. This will match match 
// "fool", "barrier", "barista" etc.

$search->condition('rating', '#\d stars?#', 'REGEX');
// Will match entries where the "rating" property matching "#\d stars?#"
// e.g: "1 star", "2 star", "3 stars" etc ( case insensitive ).

$search
  ->condition('year', 2022, '<')
  ->condition('year', 1990, '>')
  ->condition('age', 60, '<=')
  ->condition('age', 18, '>=')
  ->condition('price', [10, 50], 'BETWEEN');

$search
  ->condition('title', 'Iliad', '!=') // Different to ( case insensitive ).
  ->condition('title', ['Iliad', ' Odyssey'], 'NOT IN') // case insensitive.
  ->condition('price', [10, 50], 'NOT BETWEEN')
  ->condition('title', ['foo', 'bar'], 'UNLIKE');

$search = $file->search();
$search
  ->condition('band', 'Iron Maiden', '=')
  ->condition('release', 2000, '<');
$results = $search->find();
// Will match entries for Iron Maiden from before the yar 2000.

$search = $file->search('OR');
$search
  ->condition('band', 'Blind Guardian', '=')
  ->condition('band', 'Demons & Wizards', '=');
$results = $search->find();
// Will match entries for both Blind Guardian and Demons & Wizards.

$search = $file->search('OR');

$search->andConditionGroup()
  ->condition('band', 'Angra', '=')
  ->condition('release', 2010, '<');

$search->andConditionGroup()
  ->condition('band', 'Almah', '=')
  ->condition('release', 2013, '>');

$results = $search->find();
// Will match entries for Angra from before 2010 OR
// entries for Almah from after 2013