PHP code example of brick / std

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

    

brick / std example snippets


use Brick\Std\Iterator\CsvFileIterator;

// 1,Bob,New York
// 2,John,Los Angeles
$users = new CsvFileIterator('users.csv');

foreach ($users as [$id, $name, $city]) {
    // ...
}

use Brick\Std\Iterator\CsvFileIterator;

// id,name,city
// 1,Bob,New York
// 2,John,Los Angeles
$users = new CsvFileIterator('users.csv', true);

foreach ($users as $user) {
    // $user['id'], $user['name'], $user['city']
}

use Brick\Std\Iterator\CsvJsonFileIterator;

// 1,"Bob",["John","Mike"]
// 2,"John",["Bob","Brad"]
$users = new CsvJsonFileIterator('users.csv');

foreach ($users as [$id, $name, $friends]) {
    // $id is an int
    // $name is a string
    // $friends is an array
}

use Brick\Std\Json\JsonEncoder;

$encoder = new JsonEncoder();
$encoder->forceObject(true);

$encoder->encode(['Hello World']); // '{"0":"Hello World"}'
$encoder->encode(tmpfile()); // Brick\Std\Json\JsonException: Type is not supported

use Brick\Std\Json\JsonDecoder;

$decoder = new JsonDecoder();
$decoder->decodeObjectAsArray(true);

$decoder->decode('{"hello":"world"}'); // ['hello' => 'world']
$decoder->decode('{hello}'); // Brick\Std\Json\JsonException: Syntax error