Is there a way to fetch associative array grouped by the values of a specified column with PDO?

前端 未结 8 1442
轮回少年
轮回少年 2020-11-28 07:39

For example, let\'s use some simple data set

+---------+------+------+------------+
| name    | age  | sex  | position   |
+---------+------+------+---------         


        
8条回答
  •  旧巷少年郎
    2020-11-28 08:12

    It's quite old topic, but I found very easy solution:

    ->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_UNIQUE)
    

    First col will be set as key, rest will be set as value.

    No need to walk over the array or use array_map.

提交回复
热议问题