I that I have:
Periodo | Cliente
_________________________
jan2014 | Cliente A
jan2014 | Cliente B
I want what:
array(
'jan2014' => array(
0 => 'Cliente A',
1 => 'Cliente B',
)
)
I that I have:
Periodo | Cliente
_________________________
jan2014 | Cliente A
jan2014 | Cliente B
I want what:
array(
'jan2014' => array(
0 => 'Cliente A',
1 => 'Cliente B',
)
)
To convoke an array from the PDO in the requested structure manually create a new array ( $saida
) and add the elements according to the key ( periodo
)
//array na estrutura do PDO
$entrada = array(
0 => array('periodo' => 'jan2014', 'cliente' => 'cliente A'),
1 => array('periodo' => 'jan2014', 'cliente' => 'cliente B'),
2 => array('periodo' => 'fev2014', 'cliente' => 'cliente C'),
3 => array('periodo' => 'jan2014', 'cliente' => 'cliente D'),
4 => array('periodo' => 'mar2014', 'cliente' => 'cliente E'),
5 => array('periodo' => 'mai2014', 'cliente' => 'cliente F'),
6 => array('periodo' => 'jan2014', 'cliente' => 'cliente G'),
7 => array('periodo' => 'mai2014', 'cliente' => 'cliente H'),
8 => array('periodo' => 'jun2014', 'cliente' => 'cliente I'),
9 => array('periodo' => 'mar2014', 'cliente' => 'cliente J'),
);
$saida = array();
foreach($entrada as $item){
$saida[$item['periodo']][] = $item['cliente'];
}
the result of the new array will be:
Array
(
[jan2014] => Array
(
[0] => cliente A
[1] => cliente B
[2] => cliente D
[3] => cliente G
)
[fev2014] => Array
(
[0] => cliente C
)
[mar2014] => Array
(
[0] => cliente E
[1] => cliente J
)
[mai2014] => Array
(
[0] => cliente F
[1] => cliente H
)
[jun2014] => Array
(
[0] => cliente I
)
)
Then to get the values of the new array just use two foreachs:
foreach($saida as $key => $item){
foreach ($item as $subitem){
echo 'periodo: '. $key .' - '. $subitem .'<br>';
}
}
You need to use PDOStatement :: fetchAll
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
Array
(
[0] => Array
(
[NAME] => pear
[0] => pear
[COLOUR] => green
[1] => green
)
[1] => Array
(
[NAME] => watermelon
[0] => watermelon
[COLOUR] => pink
[1] => pink
)
)