title: PHP PDOStatement: fetch a row, as the first column as the key of an array
body: <p>I am using <code>PDOStatement</code> to query the database. Whenever I get a returned row, I want it to be fetched into an array, with the <code>$row[0]</code> as the key, and the subsequent elements in the row as the values.</p>
  <p>I can, of course, write a combination of <code>foreach</code> loops and <code>if</code> conditionals to do the job, such as the below:</p>
  <pre><code>private static function GetMySQLResult($dbname, $sqlString) {
      
      $dbh = self::ConstructPDOObject($dbname);
      $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      $result=array();
      foreach ($dbh-&gt;query($sqlString) as $row) {
          // the simplest case for 2 columns, 
          // should add more to handle more columns
          $result[$row[0]][]=$row[1];
      }
      
      return $result;   
  }
  </code></pre>
  <p>but I am looking for an existing method; is there such a method <em>already</em> exist?</p>
  <p><strong>Why reopened the question.</strong></p>
  <p>What is asked here is clearly the combination of PDO::FETCH_GROUP|PDO::FETCH_ASSOC. PDO::FETCH_KEY_PAIR only works with a 2 column result. But here the example is a 2 column result only to simplify the code in the question, not for all situations.</p>