mdref.json config
[mdref/mdref-pq] / pq / Result / map.md
1 # array pq\Result::map([mixed $keys = 0[, mixed $vals = NULL[, int $fetch_type = pq\Result::$fetchType]]])
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 * Optional int $fetch_type
12 The type the return value should have, see pq\Result::FETCH_* constants, defaults to pq\Result::$fetchType.
13
14 ## Returns:
15
16 * array|object, the mapped columns.
17
18 ## Throws:
19
20 * pq\Exception\InvalidArgumentException
21 * pq\Exception\BadMethodCallException
22 * pq\Exception\RuntimeException
23
24 ## Example:
25
26 <?php
27
28 try {
29 $connection = new pq\Connection;
30
31 $result = $connection->exec("SELECT a,b,c from generate_series(1,3) a,
32 generate_series(4,6) b,
33 generate_series(7,9) c");
34
35 foreach($result->map(array(0,1,2)) as $a => $aa) {
36 foreach ($aa as $b => $bb) {
37 foreach ($bb as $c => $res) {
38 printf("%s,%s,%s = %s ", $a, $b, $c, implode(",", $res));
39 }
40 printf("\n");
41 }
42 printf("\n");
43 }
44 } catch (\pq\Exception $e) {
45 echo $e->getMessage(), "\n";
46 }
47
48 ?>
49
50
51 It should produce:
52
53 1,4,7 = 1,4,7 1,4,8 = 1,4,8 1,4,9 = 1,4,9
54 1,5,7 = 1,5,7 1,5,8 = 1,5,8 1,5,9 = 1,5,9
55 1,6,7 = 1,6,7 1,6,8 = 1,6,8 1,6,9 = 1,6,9
56
57 2,4,7 = 2,4,7 2,4,8 = 2,4,8 2,4,9 = 2,4,9 // This should help generate maps
58 2,5,7 = 2,5,7 2,5,8 = 2,5,8 2,5,9 = 2,5,9 // of f.e. statistical data with
59 2,6,7 = 2,6,7 2,6,8 = 2,6,8 2,6,9 = 2,6,9 // some GROUP BYs etc.
60
61 3,4,7 = 3,4,7 3,4,8 = 3,4,8 3,4,9 = 3,4,9
62 3,5,7 = 3,5,7 3,5,8 = 3,5,8 3,5,9 = 3,5,9
63 3,6,7 = 3,6,7 3,6,8 = 3,6,8 3,6,9 = 3,6,9