Result Set

A League\Csv\ResultSet object represents the associated result set of processing a CSV document with a constraint builder. This object is returned from Statement::process execution.

Starting with version 9.6.0, the class implements the League\Csv\TabularDataReader interface.

Starting with version 9.22.0, the class implements the League\Csv\TabularData interface.

Instantiation

Starting with version 9.22.0

The ResultSet object can be instantiated from other objects than Statement.

You can instantiate it directly from any object that implements the League\Csv\TabularData like the Reader class:

$resultSet = ResultSet::createFromTabularData(Reader::createFromPath('path/to/file.csv'));

But you can also instantiate it from RDBMS results using the ResultSet::createFromRdbms method:

$db = new SQLite3( '/path/to/my/db.sqlite');
$stmt = $db->query("SELECT * FROM users");
$stmt instanceof SQLite3Result || throw new RuntimeException('SQLite3 results not available');

$user24 = ResultSet::createFromRdbms($stmt)->nth(23);

the createFromRdbms can be used with the following Database Extensions:

  • SQLite3 (SQLite3Result object)
  • MySQL Improved Extension (mysqli_result object)
  • PostgreSQL (PgSql\Result object returned by the pg_get_result)
  • PDO (PDOStatement object)

Beware when using the PDOStatement, the class does not support rewinding the object. As such using the instance on huge results will trigger high memory usage as all the data will be stored in a ArrayIterator instance for cache to allow rewinding and inspecting the tabular data.

Please refer to the TabularData Importer for more information.

Selecting records

Please header over the TabularDataReader documentation page for more information on the class features. If you require a more advance record selection, you should use a Statement or a FragmentFinder class to process the Reader object. The found records are returned as a ResultSet object.

Conversions

Json serialization

A dedicated JsonConverter class is added in version 9.17.0 to help converting ResultSet into proper JSON document without consuming too much memory. It is the recommended way to convert to JSON.

The ResultSet class implements the JsonSerializable interface. As such you can use the json_encode function directly on the instantiated object. The interface is implemented using PHP’s iterator_array on the ResultSet::getRecords method. As such, the returned JSON string data is affected by the presence or absence of column names.

use League\Csv\Reader;
use League\Csv\Statement;

$records = [
    ['firstname', 'lastname', 'e-mail', 'phone'],
    ['john', 'doe', 'john.doe@example.com', '0123456789'],
    ['jane', 'doe', 'jane.doe@example.com', '0123456789'],
];

$tmp = new SplTempFileObject();
foreach ($records as $record) {
    $tmp->fputcsv($record);
}

$reader = Reader::createFromFileObject($tmp)->setHeaderOffset(0);
$stmt = Statement::create()->offset(1)->limit(1);
$result = $stmt->process($reader);

echo '<pre>', PHP_EOL;
echo json_encode($result, JSON_PRETTY_PRINT), PHP_EOL;
//display
//[
//    {
//        "firstname": "jane",
//        "lastname": "doe",
//        "e-mail": "jane.doe@example.com",
//        "phone": "0123456789"
//    }
//]

The record offset is not preserved on conversion

To convert your CSV records to JSON you must be sure its content is UTF-8 encoded, using, for instance, the library CharsetConverter stream filter.

Other conversions

If you wish to convert your CSV document in XML or HTML please refer to the converters bundled with this library.