PDO fetchAll() primary key as array group key

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

So I have been working with PDO for a couple of months now, however I have come to a hitch. I want to store the contents of a specified database into an array, grouped by their primary keys. (Instead of the useless way PDO fetchAll() organises them).

My current code:

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

Which then outputs:

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

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.

So, my question is; is it possible for me to group each array by their primary key (which is id)

Thanks

回答1:

You can just use

$results = array_map('reset', $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC)) 


回答2:

This should yield what you are looking for :

$results = $pdos->fetchAll(\PDO::FETCH_UNIQUE|\PDO::FETCH_ASSOC); 


回答3:

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

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.



回答4:

I'm still suggesting you to loop using fetch() method. Otherwise, you can use array_reduce() to iterate over the array. A sample on codepad is here.

The code(in human readable form) will be:

$myFinalArray = array_reduce($myInputArray, function($returnArray, $temp) {      $temp2 = $temp['id'];      unset($temp['id']);      $returnArray[$temp2] = $temp;      return $returnArray;   } ); 


回答5:

I'd like to point out the only solution that works for me:

fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_UNIQUE|\PDO::FETCH_ASSOC);

Beware that this will strip the first column from the resultset. So the query must be:

SELECT id_keyname AS arrkey, id_keyname, .... FROM ...



回答6:

So, my question is; is it possible for me to group each array by their primary key (which is id)

Off course, you have 2 options here: Either to change the query or parse a result-set. So, I'm sure you don't want to change query itself, so I'd go with parsing result-set.

Note: You should use prepared SQL statements when they make sense. If you want to bind some parameters then its OKAY. But in this case, you only want get get result-set, so prepare() and fetch() will be kinda overdo.

So, you have:

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

And you want:

Array( [id] => Array('bar' => 'foo') ....) 

Well, you can do something like this:

$stmt = $database->dbh->query("SELECT * FROM `downloads`"); $result = array();  foreach($stmt as $array){    $result[$array['id']] = $array; }   print_r($result); // Outputs: Array(Array('id' => Array(...))) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!