Using value of a column as index in results using PDO

后端 未结 2 1302
时光取名叫无心
时光取名叫无心 2020-12-01 23:39

I have an SQL table called \'brands\' with the columns id, name, url. In that table I have this data set:

1, Solidfloor, solidfloor;
2, Quickstep, quickstep;         


        
2条回答
  •  心在旅途
    2020-12-02 00:21

    Fetch as assoc

    For the manual: http://php.net/manual/en/pdostatement.fetchall.php

    fetch_style

    Controls the contents of the returned array as documented in PDOStatement::fetch(). Defaults to value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH)

    To return an array consisting of all values of a single column from the result set, specify PDO::FETCH_COLUMN. You can specify which column you want with the column-index parameter.

    To fetch only the unique values of a single column from the result set, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_UNIQUE.

    To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.

    That last bit is key. It doesn't seem to be completely documented (that I could find), but instead of PDO::FETCH_COLUMN, you can combine PDO::FETCH_ASSOC with PDO::FETCH_GROUP to achieve the desired result:

    $PDOstmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP)
    

    So, given the above data:

    $stmt = $PDO_obj->prepare('select * from brands');
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP);
    d($result);
    

    Results in:

    array (6) [
        '1' => array (1) [
            array (2) [
                'name' => string (10) "Solidfloor"
                'url' => string (10) "solidfloor"
            ]
        ]
        '2' => array (1) [
            array (2) [
                'name' => string (9) "Quickstep"
                'url' => string (9) "quickstep"
            ]
        ]
        '4' => array (1) [
            array (2) [
                'name' => string (10) "Cleanfloor"
                'url' => string (10) "cleanfloor"
            ]
        ]
        '5' => array (1) [
            array (2) [
                'name' => string (12) "Blue Dolphin"
                'url' => string (12) "blue-dolphin"
            ]
        ]
        '6' => array (1) [
            array (2) [
                'name' => string (5) "Krono"
                'url' => string (5) "krono"
            ]
        ]
        '8' => array (1) [
            array (2) [
                'name' => string (7) "Meister"
                'url' => string (7) "meister"
            ]
        ]
    ]
    

    ( d() is just a handy debugging function from the kint library, like var_dump() or print_r() )

    Note that the column used to index the array will always be the first column in the results, so you can modify your select statement to choose which column you want. And note also that the indexed column will be stripped out of each row's array; to get around that, you can add the column twice to your select statement (i.e., select id, brands.* from brands, etc.).

    There are more parameters documented here: http://php.net/manual/en/pdostatement.fetch.php , like PDO::FETCH_UNIQUE to make sure that each index is used only once.

提交回复
热议问题