Pdo Fetchall Group Key-Value Pairs into Assoc Array

PDO fetchAll group key-value pairs into assoc array

For your problem there is pretty ready solution, that is:

$q = $db->query("SELECT `name`, `value` FROM `settings`;");
$r = $q->fetchAll(PDO::FETCH_KEY_PAIR);

Works for me, on PostgreSQL 9.1, and PHP 5.3.8 running on Windows 7 x64.

PHP PDO Fetch as multidimensional array with first column as key, and second & third as key-value pair

Basically you're stuck doing it programmatically, but you could do it with a function in the fetchAll call; using FETCH_GROUP to do the grouping by the first column and using the function to return the appropriate array from the second and third columns.

$result = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_FUNC,
function ($col2, $col3) {
return array($col2 => $col3);
});

Note this returns an array with an extra level:

Array
(
[column1 value1] => Array
(
[0] => Array
(
[column2 value1] => 360
)
)
[column1 value2] => Array
(
[0] => Array
(
[column2 value1] => 240
)
[1] => Array
(
[column2 value2] => 10
)
)
)

You can remove the extra level through a combination of array_map and array_reduce:

$result = array_map(function ($arr) {
return array_reduce($arr, function ($c, $a) {
$c[key($a)] = reset($a);
return $c;
}, []);
}, $result);

Output:

Array
(
[column1 value1] => Array
(
[column2 value1] => 360
)
[column1 value2] => Array
(
[column2 value1] => 240
[column2 value2] => 10
)
)

However it is probably simpler just to loop through the array:

$result = $stmt->fetchAll(PDO::FETCH_NUM);
$out = array();
foreach ($result as $array) {
$out[$array[0]][$array[1]] = $array[2];
}

PDO fetchAll() primary key as array group key

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.

PHP pdo get all values for a key in result set

PHP provides a handy function called array_column() for this purpose, you can pass the key as an parameter to this function call and it will return an array of columns' data,

Please make sure you're using PHP 5.5 or greater for this to work

$result_set = YOUR_RESULT_ARRAY_HERE;
print_r(array_column($result_set, 'country'));

//output
Array
(
[0] => USA
[1] => Canada
[2] => Scotland
[3] => Iceland
)

PHP - PDO fetch resultset with column as index and column as value

I don't think there's anything built-in that will do that. You can do it by using a normal fetch() loop:

$results = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$results[$row['date']] = $row['price'];
}

PDO fetch object key value as id and keep the id in the object

Just select your field twice,

SELECT id, table.* FROM table ...

Referencing to an associative array value from PDO::FETCH_ASSOC

$stmt->fetchAll() returns an array of associative arrays, because it supports returning more than one row worth of data.

Even if your result set has one row, fetchAll() still returns an array of one element, which is the associative array for the one row.

So you need:

echo $resulty[0]['title'];


Related Topics



Leave a reply



Submit