Can I get integers from a dynamic query using fetchAllKeyed? So basically I want an array of integers from a query using fetchAllKeyed for the results.   The field is an tinyint in the db so I would expect an indexed array of integers but I am getting an indexed array of strings.

Here is the database call:

$query = $this-&gt;connection
  -&gt;select('database_name', 'db')
  -&gt;fields('db', ['first_index', 'value'])
  -&gt;condition('db.value', 0, '&lt;&gt;')
  -&gt;condition('second_index', $array, 'IN')
  -&gt;execute();

$indexed_array_of_ints = $query-&gt;fetchAllKeyed();


I've tried to find other mentions of this issue and I haven't so maybe I'm just doing it wrong, I don't know.

I know that I can convert the values after the call, but I'm looking to make this operation as fast as possible so any help here would be greatly appreciated.
The issue here is that PDO returns strings, even for integer columns. Drupal handles this issue by providing the Typed Data API, which provides data typing used within Drupal, rather than PHP-level data typing.

In the Drupal 8 paradigm, database queries are generally made to query entity IDs, which are then used to load entities. Data is then retrieved from the entity. The Entity API on the back end will handle the setting/getting of the fields on the entity. The fields on the entity will all be of a Typed Data API data type, providing a consistent method of dealing with data types on the Drupal level.

The method of retrieving IDs, and loading entities is done for the sake of efficiency, as entities are cached, and may be loaded from backends such as Redis, therefore being much faster than retrieving the entity data from the database. And because fields are typed, it gives a consistent API for dealing with various data types, that can also be extended to provide additional data types through contributed modules.

So the question here becomes about what you are trying to do, and how you are trying to do it. If you are working with data that you've been saving to the database yourself, then you can switch to the Drupal way, and create a custom entity type then use the Entity API to set/get the field data. Or, if you are retrieving data from the DB that Drupal has saved there, it likely belongs to an entity that you should be retrieving, rather than directly retrieving the field data.