Converting a query result into an key=>value pairs for a lookup table My app has a number of lookup tables stored in a MySQL database that are used for various purposes such as a select element.

Unfortunately, the query results aren't in an easily-digestible format and need some massaging to be more useful to me.

For example, when you query one of the lookup tables for all values, you get an array like this:

 0 =&gt; ['level_id' =&gt; 1, 'level' =&gt; 'Trivial'],
 1 =&gt; ['level_id' =&gt; 2, 'level' =&gt; 'Moderate']
 2 =&gt; ['level_id' =&gt; 3, 'level' =&gt; 'Challenging'],
 3 =&gt; ['level_id' =&gt; 4, 'level' =&gt; 'Formidable']


But for my purposes, this is what I want:

 1 =&gt; 'Trivial',
 2 =&gt; 'Moderate',
 3 =&gt; 'Challenging',
 4 =&gt; 'Formidable'


(Note that the array keys are the same as the record id).

If the array has more than one additional column, the result will be contained in sub-arrays:

 3 =&gt; ['level' =&gt; 'Challenging', 'description' =&gt; 'Foobar'],
 4 =&gt; ['level' =&gt; 'Formidable', 'description' =&gt; 'Bazbat']


This is all very trivial, but I wrote this function that I'd like you to review to know if there is a better way to do this in PHP.

The function needs to be generic so it can use any lookup array without having to know the column names.

&lt;?php
// Turn an array into a key=&gt;value pair. Assumes the key is the first item in the sub-array.
public function column_into_keys(array $array): array {

    // get the name of the column that contains the record id
    $key = key($array[0]);

    foreach($array as $row) {

        // pop the new key off the top of the array
        $id = array_shift($row);

        // is there only one item left in the array?
        if (count($row) == 1)
            // get the first value
            $result[$id] = current($row);
        else
            // get all of the values
            $result[$id] = $row;
    }

    return $result;
}
Given you are working with PHP, there is no reason to neglect such a feature that already exists in the language.

For example when you query one of the lookup tables for all values, 

$data = $pdo-&gt;query("SELECT * FROM lookup")-&gt;fetchAll(PDO::FETCH_KEY_PAIR);


you get an array like this:

 1 =&gt; 'Trivial',
 2 =&gt; 'Moderate',
 3 =&gt; 'Challenging',
 4 =&gt; 'Formidable'


all thanks to PDO::FETCH_KEY_PAIR fetch mode.

Of course it works only with PDO, but you are supposed to use this driver anyway.