SQL行到php assoc数组

I have an SQL recordset that I wish to convert into an array that I can then change the ages without changing the db data.

I wish to be able to reference the ages by name. For example my source data is simply two columns with name and age.

// Initialise the pupils array
$query = "SELECT name, age FROM class";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$pupils = pg_fetch_all($result);
print_r($pupils);

This gives me:

Array
(
    [0] => Array
        (
            [name] => Bob
            [age] => 3
        )

    [1] => Array
        (
            [name] => Sue
            [age] => 5
        )

    [2] => Array
        (
            [name] => Mary
            [age] => 7
        )

)

What I want is:

Array
(
    [Bob] => 3,
    [Sue] => 5,
    [Mary] => 7
}

Use array_column() to build this array:

$pupils = pg_fetch_all($result);
$students = array_column($pupils, 'age', 'name');