Documentation

Result

Query result extension trait.

Provides additional result processing methods for database queries.

Table of Contents

Methods

result_keyvalue()  : array<string|int, mixed>
Get results as associative array keyed by a column value.

Methods

result_keyvalue()

Get results as associative array keyed by a column value.

public result_keyvalue([string $column = 'id' ]) : array<string|int, mixed>

Converts query results to an array where the specified column value becomes the key.

Parameters
$column : string = 'id'

Column name to use as array key. Default is 'id'.

Tags
example
$rows = $this->db
  ->select('id, name')
  ->from('users')
  ->get()
  ->result_keyvalue('id');

// Result:
// [
//   1 => ['id' => 1, 'name' => 'Oliver'],
//   2 => ['id' => 2, 'name' => 'Harry'],
// ]
throws
RuntimeException

If the specified column does not exist in results.

Return values
array<string|int, mixed>

Associative array keyed by the specified column value.


        
On this page

Search results