PDO fetchAll() 主键作为数组组键

PDO fetchAll() primary key as array group key(PDO fetchAll() 主键作为数组组键)
本文介绍了PDO fetchAll() 主键作为数组组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想将特定数据库的内容存储到按主键分组的数组中.(而不是 PDO fetchAll() 组织它们的无用方式).

I want to store the contents of a specific database into an array, grouped by their primary keys. (Instead of the useless way PDO fetchAll() organises them).

我当前的代码:

$DownloadsPDO = $database->dbh->prepare("SELECT * FROM `downloads`");
$DownloadsArray =  $DownloadsPDO->execute();
$DownloadsArray =  $DownloadsPDO->fetchAll();

然后输出:

Array ( [0] => Array ( [id] => 0 [0] => 0 [path] => /xx-xx/testfile.zip [1] => /xx-xx/testfile.zip [name] => Test Script [2] => Test Script [status] => 1 [3] => 1 ) [1] => Array ( [id] => 1 [0] => 1 [path] => /xx-xx/test--file.zip [1] => /xxxx/testfile.zip [name] => New Script-UPDATE [2] => New Script-UPDATE [status] => 1 [3] => 1 ) )

我曾考虑使用 PDO::FETCH_PAIR,但是我很快就会扩展我希望能够在此脚本上使用的数据量.这目前有效,但是当我开始扩大下载量并且更多客户端开始使用时,显然数据的分组方式会导致问题.

I was considering to use PDO::FETCH_PAIR, however I will be very soon expanding the amount of data I want to be able to use on this script. This works currently, but when I start to expand the amount of downloads and more clients come into play, obviously the way the data is grouped causes an issue.

我可以按主键(即 id)对每个数组进行分组吗?

Is it possible for me to group each array by their primary key (which is id)?

推荐答案

我决定用 fetch() 循环遍历结果,然后将它们输入到一个数组中,这是我使用过的代码并且它有效就好了:

I decided to just loop through the results with fetch() and enter them into an array as I go along, this is the code I have used and it works just fine:

$DownloadsPDO = $database->dbh->query("SELECT * FROM `downloads`");
$Array = array();
while ($d = $DownloadsPDO->fetch()) {
    $Array[$d['id']]["id"] = $d['id'];
    $Array[$d['id']]["name"] = $d['name'];
    $Array[$d['id']]["path"] = $d['path'];                          
}

// Outputs
Array ( [1] => Array ( [id] => 1 [name] => Test Script [path] => /xxxx/testfile.zip ) [2] => Array ( [id] => 2 [name] => New Script-UPDATE [path] => /xxxx/testfile.zip ) ) 

它使用主键(即id)作为数组键的名称,然后将数据添加到其中.

Which uses the primary key (being id) as the name for the array key, and then adds the data into it.

我想我会添加这个作为答案,因为这解决了它,感谢提供帮助的人,我希望这对希望实现相同目标的其他人有所帮助.

Thought I would add this as the answer as this solved it, thanks to the guys that helped out and I hope this is helpful to anyone else hoping to achieve the same thing.

这篇关于PDO fetchAll() 主键作为数组组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)