Since you are using Codeigniter, use its result set helper method, then call array_column() to convert the two columns to a flat, associative array.
var_export(
array_column(
$this->query($sql)->result(),
'name',
'id'
)
);
If you were just using PHP's native mysqli:
procedural style:
$result = mysqli_query($conn, $sql);
var_export(
array_column(
mysqli_fetch_all($result, MYSQLI_ASSOC),
'name',
'id'
)
);
object-oriented style:
$result = $conn->query($sql);
var_export(
array_column(
$conn->fetch_all($result, MYSQLI_ASSOC),
'name',
'id'
)
);
You can immediately iterate the result set object: (Demo)
$result = [];
foreach ($mysqli->query($sql) as $row) {
$result[$row['id']] = $row['name'];
}
var_export($result);
You can even use array destructuring in a body-less loop.
$result = [];
foreach ($mysqli->query($sql) as ['id' => $id, 'name' => $result[$id]]);
var_export($result);