pq\Result docs
[mdref/mdref-pq] / pq / Result / map.md
1 # array pq\Result::map([mixed $keys = 0[, mixed $vals = NULL]])
2
3 Fetch the complete result set as a simple map, a *multi dimensional array*, each dimension indexed by a column.
4
5 ## Params:
6
7 * Optional mixed $keys = 0
8 The the column indices/names used to index the map.
9 * Optional mixed $vals = NULL
10 The column indices/names which should build up the leaf entry of the map.
11
12 ## Returns:
13
14 * array, the mapped columns.
15
16 ## Example:
17
18 <?php
19
20 try {
21 $connection = new pq\Connection;
22
23 $result = $connection->exec("SELECT a,b,c from generate_series(1,3) a,
24 generate_series(4,6) b,
25 generate_series(7,9) c");
26
27 foreach($result->map(array(0,1,2)) as $a => $aa) {
28 foreach ($aa as $b => $bb) {
29 foreach ($bb as $c => $res) {
30 printf("%s,%s,%s = %s ", $a, $b, $c, implode(",", $res));
31 }
32 printf("\n");
33 }
34 printf("\n");
35 }
36 } catch (\pq\Exception $e) {
37 echo $e->getMessage(), "\n";
38 }
39
40 ?>
41
42
43 It should produce:
44
45 1,4,7 = 1,4,7 1,4,8 = 1,4,8 1,4,9 = 1,4,9
46 1,5,7 = 1,5,7 1,5,8 = 1,5,8 1,5,9 = 1,5,9
47 1,6,7 = 1,6,7 1,6,8 = 1,6,8 1,6,9 = 1,6,9
48
49 2,4,7 = 2,4,7 2,4,8 = 2,4,8 2,4,9 = 2,4,9 // This should help generate maps
50 2,5,7 = 2,5,7 2,5,8 = 2,5,8 2,5,9 = 2,5,9 // of f.e. statistical data with
51 2,6,7 = 2,6,7 2,6,8 = 2,6,8 2,6,9 = 2,6,9 // some GROUP BYs etc.
52
53 3,4,7 = 3,4,7 3,4,8 = 3,4,8 3,4,9 = 3,4,9
54 3,5,7 = 3,5,7 3,5,8 = 3,5,8 3,5,9 = 3,5,9
55 3,6,7 = 3,6,7 3,6,8 = 3,6,8 3,6,9 = 3,6,9