1
I have that I have :
Periodo | Cliente
_________________________
jan2014 | Cliente A
jan2014 | Cliente B
The one I want :
array(
'jan2014' => array(
0 => 'Cliente A',
1 => 'Cliente B',
)
)
1
I have that I have :
Periodo | Cliente
_________________________
jan2014 | Cliente A
jan2014 | Cliente B
The one I want :
array(
'jan2014' => array(
0 => 'Cliente A',
1 => 'Cliente B',
)
)
1
To query a PDO array in the manually ordered structure 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>';
}
}
0
You need to use the 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
)
)
Browser other questions tagged php array pdo multidimensional-array
You are not signed in. Login or sign up in order to post.
It didn’t work, it kept bringing all the lines.
– Bruno Rozendo
I think this is not the point of the question, it wants to bring multi-dimensional arrays and not the column name.
– Rodrigo Rigotti
Oh yes, sorry. I believe it will be necessary to create a
while
orforeach
to organize the way you want.– Ricardo