PHP code example of shimabox / smbarrayto

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

    

shimabox / smbarrayto example snippets



SMB\Arrayto;

$header = ['name', '名前', 'price'];

$rows = [
    ['apple', 'りんご', '1,000'],
    ['pineapple', 'パインアップル', '800']
];

// csv
$csv = Arrayto\Csv::factory();

// downloader object
$csvDownloader = $csv->getDownloader();

// download
$csvDownloader->setHeader($header) // optional
              ->setRows($rows) // set the rows
              ->download('example.csv');
exit;


SMB\Arrayto;

$header = ['name', '名前', 'price'];

// csv
$csv = Arrayto\Csv::factory();

// downloader object
$csvDownloader = $csv->getDownloader();

// download
$csvDownloader->setHeader($header) // optional
              ->addRow(['apple', 'りんご', '1,000']) // add the row
              ->addRow(['pineapple', 'パインアップル', '800']) // add the row
              ->download('example.csv');
exit;

  // download
  $csvDownloader->setHeader($header)
                ->setRows($rows)
                ->setToConvert(false) // Not carried out the convert
                ->download('example.csv');
  exit;


use SMB\Arrayto;

$csvHeader = ['name', '名前', 'price'];

$csvRows = [
    ['apple', 'りんご', '1,000'],
    ['pineapple', 'パインアップル', '800']
];

// csv
$csv = Arrayto\Csv::factory();

// downloader object
$csvDownloader = $csv->getDownloader();

// download
$csvDownloader->setHeader($csvHeader)
              ->setRows($csvRows)
              ->download('example.csv');
exit;


use SMB\Arrayto;

// csv
$csv = Arrayto\Csv::factory();

// outputter object
$csvOutputter = $csv->getOutputter();

// output
$csvOutputter->setHeader($csvHeader)
             ->setRows($csvRows)
             ->output();
exit;


use SMB\Arrayto;

// csv
$csv = Arrayto\Csv::factory();

// writer object
$csvWriter = $csv->getWriter();

// write
$csvWriter->setHeader($csvHeader)
          ->setRows($csvRows)
          ->setFileName('/path/to/your/example.csv') // specify the path
          ->write();


use SMB\Arrayto;

$header = ['name', '名前', 'price'];

$rows = [
    ['apple', 'りんご', '1,000'],
    ['pineapple', 'パインアップル', '800']
];

// tsv
$tsv = Arrayto\Tsv::factory();

// downloader object
$tsvDownloader = $tsv->getDownloader();

// outputter object
$tsvOutputter = $tsv->getOutputter();

// writer object
$tsvWriter = $tsv->getWriter();


use SMB\Arrayto;

$obj = new \stdClass();
$obj->hoge = 123;
$obj->piyo = ['abc' => null, 'def' => false];

$ltsvRows = [
    'time' => "[2017-01-01 08:59:60]",
    'foo' => null,
    'bar' => true,
    'buz' => 0,
    'url' => 'http://example.net',
    'arr' => ['foo' => 'bar'],
    'obj' => $obj
];

// ltsv
$ltsv = Arrayto\Ltsv::factory();

// downloader object
$ltsvDownloader = $ltsv->getDownloader();

// download
$ltsvDownloader->setRows($ltsvRows)
               ->download('example.log');
exit;


use SMB\Arrayto;

// ltsv
$ltsv = Arrayto\Ltsv::factory();

// writer object
$ltsvWriter = $ltsv->getWriter();

// write
$ltsvWriter->setRows($ltsvRows)
           ->setFileName('/path/to/your/example.log') // specify the path
           ->write();

  // write
  $ltsvWriter->setRows($ltsvRows)
             ->setFileName('/path/to/your/example.log')
             ->overrideEOL("\r\n") // CRLF
             ->write();
  


use SMB\Arrayto;

$xmlRows['book'] = [
    [
        '_attributes' => ['category' => 'children', 'currency' => 'USD'],
        'tilte' => [
            '_attributes' => ['lang' => 'en'],
            '_value' => 'Harry Potter'
        ],
        'author' => 'J K. Rowling',
        'year' => 2005,
        'price' => 29.99
    ],
    [
        '_attributes' => ['category' => 'music', 'currency' => 'JPY'],
        'tilte' => [
            '_attributes' => ['lang' => 'ja'],
            '_value' => '[score] Boys&Gilrs'
        ],
        'author' => 'GOING STEADY(銀杏BOYZ)',
        'year' => 2000,
        'price' => "2,808"
    ]
];

// xml
$xml = Arrayto\Xml::factory();

// downloader object
$xmlDownloader = $xml->getDownloader();

// download
$xmlDownloader->setRows($xmlRows)
              ->setRootElementName('bookstore') // optional
              ->download('example.xml');
exit;


use SMB\Arrayto;

// xml
$xml = Arrayto\Xml::factory();

// writer object
$xmlWriter = $xml->getWriter();

// write
$xmlWriter->setRows($xmlRows)
          ->setRootElementName('bookstore') // optional
          ->setFileName('/path/to/your/example.xml') // specify the path
          ->write();

    $xmlWriter->setRows($xmlRows)
              ->setRootElementName('bookstore') // optional
              ->setFileName('example.xml') // specify the path
              ->toFormatOutput(false) // it does not format
              ->write();
  


use SMB\Arrayto;

$obj = new \stdClass();
$obj->hoge = '123';
$obj->piyo = ['abc' => null, 'def' => false];

$jsonRows = [
    ['key1' => null, 'key2' => true, 'key3' => 0],
    ['url' => 'http://example.net'],
    ['arr' => ['foo' => 'bar']],
    ['obj' => $obj]
];

// json
$json = Arrayto\Json::factory();

// downloader object
$jsonDownloader = $json->getDownloader();

// download
$jsonDownloader->setRows($jsonRows)
               ->download('example.json');
exit;


use SMB\Arrayto;

// json
$json = Arrayto\Json::factory();

// outputter object
$jsonOutputter = $json->getOutputter();

// output
$jsonOutputter->setRows($jsonRows)
              ->output();
exit;


use SMB\Arrayto;

// json
$json = Arrayto\Json::factory();

// writer object
$jsonWriter = $json->getWriter();

// write
$jsonWriter->setRows($jsonRows)
           ->setFileName('/path/to/your/example.json') // specify the path
           ->write();

    $jsonWriter->setRows($jsonRows)
               ->setFileName('/path/to/your/example.json') // specify the path
               ->setJsonEncodeOption(JSON_FORCE_OBJECT) // override
               ->write();
  

    
    use SMB\Arrayto;

    // csv
    $csv = Arrayto\Csv::factory();

    // downloader object
    $csvDownloader = $csv->getDownloader();

    // download an existing file
    $csvDownloader->downloadExistsFile('/path/to/your/example.csv'); // file name is example.csv

    // or download an existing file as an alias
    $csvDownloader->downloadExistsFile('/path/to/your/example.csv', 'sample.csv'); // file name is sample.csv

    exit;
  

    
    use SMB\Arrayto;

    $rows = [
        ['apple', 'りんご', '1,000'],
        ['pineapple', 'パインアップル', '800']
    ];

    // csv
    $csv = Arrayto\Csv::factory();

    // writer object
    $csvWriter = $csv->getWriter()
                     ->setRows($rows)
                     ->setFileName('/path/to/your/example.csv'); // specify the path

    // downloader object
    $csvDownloader = $csv->getDownloader();

    // download after the writing of the file
    $csvDownloader->downloadExistsFileUsingWriter('sample.csv', $csvWriter); // file name is sample.csv

    exit;
  

  use SMB\Arrayto;

  $obj = new \stdClass();
  $obj->hoge = '123';
  $obj->piyo = ['abc' => null, 'def' => false];

  // json
  $json = Arrayto\Json::factory();

  // write
  $json->getWriter()
	   ->addRow(['key1' => null, 'key2' => true, 'key3' => 0])
       ->addRow(['url' => 'http://example.net'])
       ->addRow(['arr' => ['foo' => 'bar']])
       ->addRow(['obj' => $obj])
       ->setFileName('/path/to/your/example.json')
       ->write();
  

    
    use SMB\Arrayto;

    $obj = new \stdClass();
    $obj->hoge = '123';
    $obj->piyo = ['abc' => null, 'def' => false];

    $obj2 = new \stdClass();
    $obj2->hoge = '456';
    $obj2->piyo = ['ghi' => null, 'jkl' => false];

    // json
    $json = Arrayto\Json::factory();

    // write
    $json->getWriter()
         ->addRowBySpecifyingKV('url', 'http://example.net')
         ->addRowBySpecifyingKV('url', 'http://example.org')
         ->addRowBySpecifyingKV('arr', ['foo' => 'bar'])
         ->addRowBySpecifyingKV('arr', ['baz' => 'fuga'])
         ->addRowBySpecifyingKV('obj', $obj)
         ->addRowBySpecifyingKV('obj', $obj2)
         ->setFileName('/path/to/your/example.json')
         ->write();
  

    
    use SMB\Arrayto;

    $obj = new \stdClass();
    $obj->hoge = '123';
    $obj->piyo = ['abc' => null, 'def' => false];

    $obj2 = new \stdClass();
    $obj2->hoge = '456';
    $obj2->piyo = ['ghi' => null, 'jkl' => false];

    // json
    $json = Arrayto\Json::factory();

    // write
    $json->getWriter()
         ->setAllowDuplicateKey(false) // it does not allow duplicate keys
         ->addRowBySpecifyingKV('url', 'http://example.net')
         ->addRowBySpecifyingKV('url', 'http://example.org')
         ->addRowBySpecifyingKV('arr', ['foo' => 'bar'])
         ->addRowBySpecifyingKV('arr', ['baz' => 'fuga'])
         ->addRowBySpecifyingKV('obj', $obj)
         ->addRowBySpecifyingKV('obj', $obj2)
         ->setFileName('/path/to/your/example.json')
         ->write();
  

    
    use SMB\Arrayto;

    $header = ['name', '名前', 'feature'];

    $rows1 = [
        ['apple', 'りんご', "Sweet\tRed"]
    ];

    // tsv
    $tsv = Arrayto\Tsv::factory();

    // writer object
    $tsvWriter = $tsv->getWriter();

    $tsvWriter->setHeader($header)
              ->setRows($rows1)
              ->setFileName('/path/to/your/example.tsv')
              ->write();

    $rows2 = [
        ['pineapple', 'パインアップル', "Sour\tYellow"]
    ];
    $tsvWriter->clearHeader()
             ->setRows($rows2)
             ->setOpenMode('a') // set open mode 'a'
             ->write();

    $rows3 = [
        ['orange', 'オレンジ', "Juicy\tOrange"]
    ];
    $tsvWriter->setRows($rows3)
              ->write();
  

    $csvWriter->setRows($rows)
              ->setPermission(777) // set the permissions to 777
              ->write();
  

  
  use SMB\Arrayto;

  $csv = Arrayto::factory(Arrayto::CSV);   // => SMB\Arrayto\Csv
  // $csv == Arrayto\Csv::factory();

  $tsv = Arrayto::factory(Arrayto::TSV);   // => SMB\Arrayto\Tsv
  // $tsv == Arrayto\Tsv::factory();

  $ltsv = Arrayto::factory(Arrayto::LTSV); // => SMB\Arrayto\Ltsv
  // $ltsv == Arrayto\Ltsv::factory();

  $xml = Arrayto::factory(Arrayto::XML);   // => SMB\Arrayto\Xml
  // $xml == Arrayto\Xml::factory();

  $json = Arrayto::factory(Arrayto::JSON); // => SMB\Arrayto\Json
  // $json == Arrayto\Json::factory();

  
  use SMB\Arrayto;

  $csvDownloader = new Arrayto\Plugins\Csv\Downloader();
  // $csvDownloader == Arrayto\Csv::factory()->getDownloader();
  $csvOutputter = new Arrayto\Plugins\Csv\Outputter();
  // $csvOutputter == Arrayto\Csv::factory()->getOutputter();
  $csvWriter = new Arrayto\Plugins\Csv\Writer();
  // $csvWriter == Arrayto\Csv::factory()->getWriter();

  $tsvDownloader = new Arrayto\Plugins\Tsv\Downloader();
  // $tsvDownloader == Arrayto\Tsv::factory()->getDownloader();
  $tsvOutputter = new Arrayto\Plugins\Tsv\Outputter();
  // $tsvOutputter == Arrayto\Tsv::factory()->getOutputter();
  $tsvWriter = new Arrayto\Plugins\Tsv\Writer();
  // $tsvWriter == Arrayto\Tsv::factory()->getWriter();

  $ltsvDownloader = new Arrayto\Plugins\Ltsv\Downloader();
  // $ltsvDownloader == Arrayto\Ltsv::factory()->getDownloader();
  $ltsvWriter = new Arrayto\Plugins\Ltsv\Writer();
  // $ltsvWriter == Arrayto\Ltsv::factory()->getWriter();

  $xmlDownloader = new Arrayto\Plugins\Xml\Downloader();
  // $xmlDownloader == Arrayto\Xml::factory()->getDownloader();
  $xmlWriter = new Arrayto\Plugins\Xml\Writer();
  // $xmlWriter == Arrayto\Xml::factory()->getWriter();

  $jsonDownloader = new Arrayto\Plugins\Json\Downloader();
  // $jsonDownloader == Arrayto\Json::factory()->getDownloader();
  $jsonOutputter = new Arrayto\Plugins\Json\Outputter();
  // $jsonOutputter == Arrayto\Json::factory()->getOutputter();
  $jsonWriter = new Arrayto\Plugins\Json\Writer();
  // $jsonWriter == Arrayto\Json::factory()->getWriter();
download($fileName);
 setHeader(array $header); 
 setRootElementName($name); 
 setReplaceSpacesByUnderScoresInKeyNames($bool); 
 downloadExistsFile($fileName, $aliasOfFileName = ''); 
 downloadExistsFileUsingWriter($fileName, Writable $writer); 
 addRow($row); 
 addRowBySpecifyingKV($key, $value); 
 setFileName($fileName); 
 Arrayto\Plugins\XXX\XXX