PHP code example of cerbero / lazy-json

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

    

cerbero / lazy-json example snippets


LazyCollection::fromJson($source, 'data.*.users.*')
    ->map($this->mapToUser(...))
    ->filter($this->filterUser(...))
    ->values()
    ->chunk(1_000)
    ->each($this->storeUsersChunk(...));

use Cerbero\LazyJson\LazyJson;
use Illuminate\Support\LazyCollection;

use function Cerbero\LazyJson\lazyJson;

// auto-registered lazy collection macro
$lazyCollection = LazyCollection::fromJson($source);

// static method
$lazyCollection = LazyJson::from($source);

// namespaced helper
$lazyCollection = lazyJson($source);

LazyCollection::fromJson($source)
    ->values()
    ->map(/* ... */)
    ->where(/* ... */)
    ->each(/* ... */);

$source = 'https://randomuser.me/api/1.4?seed=json-parser&results=5';

$dot = 'results.*.location.city';

LazyCollection::fromJson($source, $dot)->each(function (string $value, string $key) {
    // 1st iteration: $key === 'city', $value === 'Sontra'
    // 2nd iteration: $key === 'city', $value === 'San Rafael Tlanalapan'
    // 3rd iteration: $key === 'city', $value === 'گرگان'
    // ...
});

$dots = ['results.*.gender', 'results.*.email'];

LazyCollection::fromJson($source, $dots)->each(function (string $value, string $key) {
    // 1st iteration: $key === 'gender', $value === 'female'
    // 2nd iteration: $key === 'email', $value === '[email protected]'
    // 3rd iteration: $key === 'gender', $value === 'female'
    // 4th iteration: $key === 'email', $value === '[email protected]'
    // ...
});