PHP code example of bcncommerce / json-stream

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

    

bcncommerce / json-stream example snippets


$fh = fopen($filename, "w");
$writer = new Writer($fh);

$writer->enter(Writer::TYPE_OBJECT);                // enter root object
    $writer->write("catalog", $catalog['id']);      // write key-value entry
    $writer->enter("items", Writer::TYPE_ARRAY);    // enter items array
        foreach($catalog['products'] as $product) {
            $writer->write(null, array(             // write an array item
                'sku'  => $product['sku'],
                'name' => $product['name']
            ));
        }
    $writer->leave();                               // leave items array
$writer->leave();                                   // leave root object

fclose($fh);

$fh = fopen($filename, "r");

$reader = new Reader($fh);
$reader->enter(Reader::TYPE_OBJECT);                // enter root object
    $catalog['id'] = $reader->read("catalog");      // read catalog node
    $reader->enter("items", Reader::TYPE_ARRAY);    // enter item array
        while($product = $reader->read()) {         // read product structure
            $catalog['products'][] = $product;
        }
    $reader->leave();                               // leave item node
$reader->leave();                                   // leave root object

fclose($fh);