function PrefetchedResult::fetchAllKeyed

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/Statement/PrefetchedResult.php \Drupal\Core\Database\Statement\PrefetchedResult::fetchAllKeyed()

File

core/lib/Drupal/Core/Database/Statement/PrefetchedResult.php, line 84

Class

PrefetchedResult
Class for prefetched results of a data query language (DQL) statement.

Namespace

Drupal\Core\Database\Statement

Code

public function fetchAllKeyed(int $keyIndex = 0, int $valueIndex = 1) : array {
  if (!isset($this->columnNames[$keyIndex]) || !isset($this->columnNames[$valueIndex])) {
    return [];
  }
  $key = $this->columnNames[$keyIndex];
  $value = $this->columnNames[$valueIndex];
  $result = [];
  while ($row = $this->fetch(FetchAs::Associative, $this->fetchOptions)) {
    $result[$row[$key]] = $row[$value];
  }
  return $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.