C
C
Caf2018-03-20 10:40:58
PHP
Caf, 2018-03-20 10:40:58

How to get array: key from first column, value from second?

Good afternoon! When executing a query like this:

$stmt->prepare("SELECT column_1, column_2 FROM table");
$stmt->execute();
$result_array = $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP);

We get an array of the form:
array {
  [column_1_value]=>
  array(1) {
    [0]=>
    string(8) "column_2_value"
  }
  [column_1_value]=>
  array(1) {
    [0]=>
    string(8) "column_2_value"
  }
  ...
}

This is not exactly what I need, but I need the following result:
array {
  [column_1_value]=> string(8) "column_2_value"
  [column_1_value]=> string(8) "column_2_value"
  ...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Hog, 2018-03-20
@Caf

$stmt->fetchAll(PDO::FETCH_KEY_PAIR);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question