PHP code example of jspit / tablearray

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

    

jspit / tablearray example snippets


use Jspit\TableArray;
VERSION);  //string(5) "2.6.1"

use Jspit\TableArray;
VERSION);  //string(5) "2.6.1"

$data = [ 
  ['id' => 1, 'val' => 23.333333333], 
  ['id' => 2, 'val' => 13.7777777777], 
]; 
$newData = TableArray::create($data) 
  ->select('id, FORMAT("%6.2f",val) as rval') 
  ->orderBy('val ASC')
  ->fetchAll(); 
  
$expected = [ 
  ['id' => 2, 'rval' => " 13.78"],
  ['id' => 1, 'rval' => " 23.33"],  
]; 
var_dump($newData == $expected); //bool(true)

$data = [ 
  ['name' => 'A1', 'likes' => 3], 
  ['name' => 'A12', 'likes' => 6], 
  ['name' => 'A2','likes' => 14], 
  ['name' => 'A14','likes' => 7], 
];
 
$newData = TableArray::create($data)
  ->select('name AS class, likes') 
  ->orderBy('name ASC NATURAL') 
  ->fetchAll();

$expected = [ 
  ['class' => 'A1', 'likes' => 3], 
  ['class' => 'A2','likes' => 14], 
  ['class' => 'A12', 'likes' => 6], 
  ['class' => 'A14','likes' => 7], 
];
var_dump($newData === $expected); //bool(true)

$data = [ 
  ['group' => 1, 'type' => 'A', 'value' => 'AA'],
  ['group' => 2, 'type' => 'A', 'value' => 'BB'], 
  ['group' => 1, 'type' => 'B', 'value' => 5],
  ['group' => 2, 'type' => 'B', 'value' => 7], 
]; 
$newData = TableArray::create($data) 
  ->pivot('group','value','type')
  ->fetchAll();

$expected = [
  1 => ['group' => 1, 'value.A' => "AA", 'value.B' => 5 ],
  2 => ['group' => 2, 'value.A' => "BB", 'value.B' => 7 ],
];


TableArray::setCsvDefaultOptions([
  'delimiter'=>',',
  'title => true',  //use first row as keys
]);
$data = TableArray::createFromCsvFile('file.csv')
  ->fetchAll()
;

$data = [ 
  ['id' => "1",'group' => 1, 'value' => 2, 'value2' => 3], 
  ['id' => "2",'group' => 2, 'value' => 4, 'value2' => 7],
  ['id' => "3",'group' => 1, 'value' => 1, 'value2' => 2], 
  ['id' => "4",'group' => 2, 'value' => 6, 'value2' => 8],
];

$newData = TableArray::create($data)
  ->filterGroupAggregate(['value' => 'MAX', 'value2' => 'AVG'],['group'])
  ->orderBy('value2 DESC')
  ->fetchAll();

$expected = [ 
  ['id' => "4",'group' => 2, 'value' => 6, 'value2' => 7.5],
  ['id' => "1",'group' => 1, 'value' => 2, 'value2' => 2.5], 
];
var_dump($newData === $expected);  //bool(true) 

$data =[
  ['id' => 1, 'article' => "pc1", 'price' => 1231.0],
  ['id' => 1, 'article' => "pc2", 'price' => 471.5],
];

$newData = TableArray::create($data)
  ->select("article as Name, FORMAT('%.2f€',price) as Euro")
  ->fetchAll()
;
/* Result $newData
[
  ['Name' => "pc1", 'Euro' => "1231.00€"],
  ['Name' => "pc2", 'Euro' => "471.50€",
]
*/