PDO fetchAll() primary key as array group key

后端 未结 6 880
南笙
南笙 2020-12-10 04:26

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

My curr

相关标签:
6条回答
  • 2020-12-10 04:47

    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;
      }
    );
    
    0 讨论(0)
  • 2020-12-10 04:48

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

    0 讨论(0)
  • 2020-12-10 04:49

    This should yield what you are looking for :

    $results = $pdos->fetchAll(\PDO::FETCH_UNIQUE|\PDO::FETCH_ASSOC);
    
    0 讨论(0)
  • 2020-12-10 04:56

    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.

    0 讨论(0)
  • 2020-12-10 04:58

    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(...)))
    
    0 讨论(0)
  • 2020-12-10 05:05

    You can just use

    $results = array_map('reset', $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC))
    
    0 讨论(0)
提交回复
热议问题