Kotchasan Framework Documentation

Kotchasan Framework Documentation

ArrayResult

EN 05 Feb 2026 06:23

ArrayResult

\Kotchasan\Result\ArrayResult is a ResultInterface implementation for array-based results, primarily used with cached queries.

Creation

use Kotchasan\Result\ArrayResult;

$data = [
    ['id' => 1, 'name' => 'John'],
    ['id' => 2, 'name' => 'Jane']
];

// Array format (default)
$result = new ArrayResult($data);

// Object format
$result = new ArrayResult($data, 'object');

Constructor

public function __construct(array $data, string $resultFormat = 'array')
Parameter Type Description
$data array Array of rows
$resultFormat string Format: 'array' (default) or 'object'

Result Formats

Array Format (Default)

$result = new ArrayResult($data);

$row = $result->fetch();
echo $row['id'];
echo $row['name'];

Object Format

$result = new ArrayResult($data, 'object');

$row = $result->fetch();
echo $row->id;
echo $row->name;

Methods

Standard Methods (from ResultInterface)

Method Return Description
fetch() mixed Fetch next row
fetchAll() array Fetch all remaining rows
fetchColumn($col) mixed Fetch single column
rowCount() int Count rows
columnCount() int Count columns
getColumnMeta($col) array Column metadata
getResult() array Raw data array
isEmpty() bool Check if empty
hasMore() bool Check if more rows exist
reset() bool Reset pointer
close() bool Clear data
count() int Count rows

Additional Methods

Method Return Description
first() mixed Fetch first row (does not move pointer)
all() array Fetch all (does not move pointer)
getData() array Get raw array
getResultFormat() string Get result format

Iterator Methods

Method Description
current() Current row
key() Current position
next() Move to next row
rewind() Move to first row
valid() Check if data exists at current position

Usage Examples

Basic Usage

$data = [
    ['id' => 1, 'name' => 'John', 'email' => 'john@example.com'],
    ['id' => 2, 'name' => 'Jane', 'email' => 'jane@example.com']
];

$result = new ArrayResult($data);

// Fetch row by row
while ($row = $result->fetch()) {
    echo $row['name'] . "\n";
}

Using first() and all()

$result = new ArrayResult($data, 'object');

// Fetch first row (pointer stays)
$first = $result->first();
echo $first->name;  // John

// Fetch all (pointer stays)
$all = $result->all();
echo count($all);  // 2

Using foreach (Iterator)

$result = new ArrayResult($data);

foreach ($result as $index => $row) {
    echo "{$index}: {$row['name']}\n";
}

Column Metadata

$result = new ArrayResult($data);

$meta = $result->getColumnMeta(0);
// ['name' => 'id', 'native_type' => 'STRING']

echo $result->columnCount();  // 3 (id, name, email)

Reset and Reread

$result = new ArrayResult($data);

$all1 = $result->fetchAll();  // Fetch all, pointer at end
$result->reset();              // Reset
$all2 = $result->fetchAll();  // Fetch again

Close (Free Memory)

$result = new ArrayResult($data);
// Use it...

$result->close();  // Clear data array, free memory

Use Cases

Cached Query Results

// Query cache returns ArrayResult
$cached = $cache->get('users_query');

if ($cached) {
    $result = new ArrayResult($cached);
} else {
    $result = $db->select('*')->from('user')->execute();
    $cache->set('users_query', $result->fetchAll());
}

Mock Results for Testing

// Create mock result for unit test
$mockData = [
    ['id' => 1, 'name' => 'Test User']
];

$result = new ArrayResult($mockData, 'object');
$user = $result->first();

$this->assertEquals('Test User', $user->name);

Converting PDOResult to ArrayResult

// Fetch from database
$pdoResult = $db->select('*')->from('user')->execute();
$data = $pdoResult->fetchAll();
$pdoResult->close();

// Convert to ArrayResult for multiple iterations
$arrayResult = new ArrayResult($data);

// Iterate multiple times
foreach ($arrayResult as $user) { ... }
$arrayResult->reset();
foreach ($arrayResult as $user) { ... }

Internal Properties

Property Type Description
$data array Raw data array
$position int Current position (0-indexed)
$resultFormat string 'array' or 'object'