PHP code example of mya-zaki / csvert

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

    

mya-zaki / csvert example snippets



namespace App;

use MyaZaki\Csvert\Record;

class PostalCode extends Record
{
    public $delimiter = ',';
    public $enclosure = '"';
    public $escape = '\\';

    public $charset = 'SJIS-win';

    public $header = true;

    public $columns = [
        'Code',
        'Street',
        'City',
        'State',
    ];

    public function getAddress()
    {
        return $this->attributes['Street'] . ', ' . $this->attributes['City'] . ', ' . $this->attributes['State'];
    }
}

$parser = PostalCode::parse($filepath);
$address_list = [];
$parser->walk(function ($record) use (&$address_list) { // Call user function each line
    // Given Record instance
    $address_list[] = $record['Code'] . ' ' . $record->getAddress();
});

$parser = PostalCode::parseString($csv_content);
$records = $parser->get(); // Get Collection
$address = $records->where('State', '北海道')->first();
var_dump($address['Code']);