Reputation: 2508
I have following code
$stmt = $pdo->query("SELECT Name,office FROM `table`");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);
Which prints the following
Array (
[0] => Array (
[Name] => Suki Burks
[office] => London
)
[1] => Array (
[Name] => Thor Walton
[office] => New York
)
)
And I would like numeric indexed array, like this
Array (
[0] => Array (
[0] => Suki Burks
[1] => London
)
[1] => Array (
[0] => Thor Walton
[1] => New York
)
)
I tried to find a PDO constant for this So that I could use it like this
$stmt->fetchALL(PDO::FETCH_INEDXED)
But i think there is no inbuilt method in PDO to achieve this, but what would be the good method to achieve this?
Thanks
Upvotes: 2
Views: 1536
Reputation: 26450
The manual on fetchAll()
states about the first argument, fetch_style
, as follows (emphasis mine)
Controls the contents of the returned array as documented in
PDOStatement::fetch()
. Defaults to value ofPDO::ATTR_DEFAULT_FETCH_MODE
(which defaults toPDO::FETCH_BOTH
)
Which means that for the full list of constants available for different fetch-styles, you should look in the fetch()
documentation. This shows that for numeric indexed array, you should use PDO::FETCH_NUM
, which is described as..
PDO::FETCH_NUM
: returns an array indexed by column number as returned in your result set, starting at column 0
The end result would look like this, all that was changes was the constant supplied as the argument to fetchAll()
.
$stmt = $pdo->query("SELECT name,office FROM `table`"); // Singlequotes are for values, backticks for table/column-names
$rows = $stmt->fetchAll(PDO::FETCH_NUM);
print_r($rows);
Upvotes: 5