PDO关联数组 - 联想回报 [英] PDO associative arrays - return associative

查看:148
本文介绍了PDO关联数组 - 联想回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

$dbInstance = DB_Instance::getDBO();
$statement = $dbInstance->prepare("SELECT id, name FROM language ORDER BY id");
$statement->execute();      
$rows = $statement->fetchAll(); 

//Create associative array wuth id set as an index in array
$languages = array();
foreach($rows as $r) {
    $languages[$r['id']] = $r['name'];
}
return $languages;

我无法弄清楚如何使用PDO语句来达到同样的结果数组$语言产生。我已经尝试了一些不同的fetch_styles。

I can't figure out how to use PDO-statement to achieve the same result that array $languages produces. I've tried some different fetch_styles.

我已经尝试了一些不同的风格,我能得到这样的:

I've tried some different styles and I could get like:

[0] svenska
[1] engelska

但我想这样的:

[1] svenska
[2] engelska

(其中,1和2是在数据库ID的值)

(where 1 and 2 are the values of id in database)

我想我可以创建一个函数并调用与 FETCH_FUNC ,但我不知道那会是如此之大任。

I guess I could create a function and call that with FETCH_FUNC but I'm not sure that would be so great either.

是最好的/干净的方式上面做呢?

Is the above the best/cleanest way to do it?

推荐答案

不是真的知道是否有什么更好的办法。你可以试试这个?

Not really sure if there's any better way. You could try this?

$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

$languages = array();

function getLangs($col, $row) {
     $languages[$col['id']] = $col['name'];
}

array_walk($rows, 'getLangs');

有没有错,foreach循环。其实我用你得到了什么。很难比这更清洁的...

There's nothing wrong with foreach loops. I'd actually use what you've got. It's hard to get cleaner than that...

更新:

经过仔细重新阅读你的问题,你真正应该问的是你是否能以这样一种方式,结果在不同的格式返回格式化您的查询。

After carefully re-reading your question, what you REALLY should be asking is whether you can format your QUERY in such a way that the results are returned in a different format.

这将返回正常的SELECT查询的方式是正是如此:

The way that your normal SELECT query is returned is thusly:

+----+----------+
| id |     name |
+----+----------+
|  1 |  svenska |
|  2 | engelska |
| .. |      ... |
| .. |      ... |
+----+----------+

$row = array(
    row_1 => array(
        id   => "1",
        name => "svenska"
    ),
    row_2 => array(
        id   => "2",
        name => "engelska"
    ),
    row_2 => array(
        id   => "...",
        name => "..."
    ),
    row_2 => array(
        id   => "...",
        name => "..."
    )
)

$row[$row_number][$column_name] = $value

什么你问的是一些方法来回报你这样的查询结果:

What you're asking for is for some way to return your query results like THIS:

// Query result is only one row, with each 'id' as column name
// And the 'name' from the same row as it's value...

+---------+----------+-----+-----+-----+
|       1 |        2 | ... | ... | ... |
+---------+----------+-----+-----+-----+
| svenska | engelska | ... | ... | ... |
+---------+----------+-----+-----+-----+

$row = array(
    row_1 => array(
          1 => "svenska",
          2 => "engelska",
        ... => "...",
        ... => "...",
        ... => "..."
    )
)

$languages = $row[row_1];
$languages[$id] = $name;

我不完全相信你可以do这在SQL中,是完全诚实。我也想,即使你能推荐反对它。这将是可怕的缩放表。如果您的表是静态的,那么为什么不是在我刚才提到的,开始用的方式对其进行格式化?为什么不把它在静态PHP数组包含文件?

I'm not entirely sure you CAN do this in SQL, to be perfectly honest. I would also recommend against it, even if you could. It would be horrendous for a scaling table. If your table is static, then why not format it in the way I just mentioned to begin with? Why not just have it in a static PHP array in an include file?

这篇关于PDO关联数组 - 联想回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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