在使用PDO的结果中使用列的值作为索引 [英] Using value of a column as index in results using PDO

查看:65
本文介绍了在使用PDO的结果中使用列的值作为索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为'brands'的SQL表,其中包含ID,名称,URL列. 在该表中,我有以下数据集:

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;
4, Cleanfloor, cleanfloor;
5, Blue Dolphin, blue-dolphin;
6, Krono, krono;
8, Meister, meister;

我现在正在全部获取它们,并且得到一个不错的数组作为回报,但是,我需要数组的索引不是一个递增的数字,而是那个特定行的ID.我当然可以遍历结果集,但是有没有更干净的方法呢?

I'm fetching them all right now and I get a nice array in return, but, I need the index of the arrays to not be an incremented number, but the id of that particular row. I could of course loop through the result set, but is there a cleaner way of doing this?

推荐答案

以assoc身份获取

对于手册: http://php.net/manual/en/pdostatement.fetchall.php

fetch_style

fetch_style

如PDOStatement :: fetch()中所述控制返回数组的内容.默认为PDO :: ATTR_DEFAULT_FETCH_MODE的值(默认为PDO :: FETCH_BOTH)

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)

要从结果集中返回由单个列的所有值组成的数组,请指定PDO :: FETCH_COLUMN.您可以使用column-index参数指定所需的列.

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.

要仅从结果集中获取单个列的唯一值,请对PDO :: FETCH_COLUMN与PDO :: FETCH_UNIQUE进行按位或运算.

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

要返回按指定列的值分组的关联数组,请对PDO :: FETCH_COLUMN与PDO :: FETCH_GROUP进行按位或运算.

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

最后一点是关键.它似乎没有完全记录(我可以找到),但是可以将PDO :: FETCH_ASSOC与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)

因此,鉴于上述数据:

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

结果:

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()只是kint库中一个方便的调试功能,例如var_dump()或print_r())

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

请注意,用于索引数组的列将始终是结果中的第一列,因此您可以修改select语句以选择所需的列.还要注意,索引列将从每行的数组中删除;要解决此问题,您可以将列两次添加到select语句(即select id, brands.* from brands等).

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.).

此处记录了更多参数: http://php.net/manual/en/pdostatement.fetch. php ,就像PDO :: FETCH_UNIQUE一样,以确保每个索引仅使用一次.

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.

这篇关于在使用PDO的结果中使用列的值作为索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆