0
People want to create a separate array in groups by date of the result of a query made in DB
example:
// [...]
$sql = $this->db->query($query) or die (sprintf("Falha: %s", $this->db->error()));
if ($sql->num_rows) {
while ($row = $sql->fetch_object()) {
$data[] = $row;
}
}
// [...]
test example:
$data[] = array(
'data' => '2015-02-05',
'refer' => 'X741852',
'favorecido' => 'MARIA',
'debito' => '2.500'
);
$data[] = array(
'data' => '2015-02-05',
'refer' => 'B890656',
'favorecido' => 'EDUARDA',
'debito' => '500'
);
$data[] = array(
'data' => '2015-02-18',
'refer' => 'CK546045',
'favorecido' => 'JOAO',
'debito' => '42.050'
);
$data[] = array(
'data' => '2014-06-09',
'refer' => 'FE55852',
'favorecido' => 'CHARLES',
'debito' => '28.500'
);
$data[] = array(
'data' => '2014-06-09',
'refer' => 'X741852',
'favorecido' => 'VIVIANE',
'debito' => '74.2500'
);
exit:
Array
(
[0] => Array
(
[data] => 2015-02-05
[refer] => X741852
[favorecido] => MARIA
[debito] => 2.500
)
[1] => Array
(
[data] => 2015-02-05
[refer] => B890656
[favorecido] => EDUARDA
[debito] => 500
)
[2] => Array
(
[data] => 2015-02-18
[refer] => CK546045
[favorecido] => JOAO
[debito] => 42.050
)
[3] => Array
(
[data] => 2014-06-09
[refer] => FE55852
[favorecido] => CHARLES
[debito] => 28.500
)
[4] => Array
(
[data] => 2014-06-09
[refer] => X741852
[favorecido] => VIVIANE
[debito] => 74.2500
)
)
Intended Result:
Array
(
[2015-02-05] => Array
(
[0] => Array
(
[data] => 2015-02-05
[refer] => X741852
[favorecido] => MARIA
[debito] => 2.500
)
[1] => Array
(
[data] => 2015-02-05
[refer] => B890656
[favorecido] => EDUARDA
[debito] => 500
)
)
[2015-02-18] => Array
(
[2] => Array
(
[data] => 2015-02-18
[refer] => CK546045
[favorecido] => JOAO
[debito] => 42.050
)
)
[2014-06-09] => Array
(
[3] => Array
(
[data] => 2014-06-09
[refer] => FE55852
[favorecido] => CHARLES
[debito] => 28.500
)
[4] => Array
(
[data] => 2014-06-09
[refer] => X741852
[favorecido] => VIVIANE
[debito] => 74.2500
)
)
)
Thank you Bacco, I chose your solution for being simpler
– smigol
@Smigol noticed the difference in i++ right? In your example, the internal indices continue 1, 2, group changes 3, 4, 5, etc. If you do not need it, you can remove the
$i
of the play– Bacco
Yeah, yeah, see man. Now to with another doubt I had thought to create this group by date because I think it is simpler to list the report, but I’m not able to ;( Think better create a new post or it’s okay to add to this post?
– smigol
@smigol as it already has two answers, I think better separate. But take care to make it very specific to the problem you want to solve. Instead of asking questions about what you are trying to do, prioritize demonstrating the result where you are going. Sometimes when you’re just part of the code, you stop getting great ideas and fall into the XY problem
– Bacco
Blz @Bacco, I’ll do it ;)
– smigol