Create a separate array by date groups

0

I 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;
    }
}
// [...]

Sample Test:

$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'
);

output:

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
                )
        )

)
    
asked by anonymous 13.01.2016 / 12:44

2 answers

3

Basically you just need it:

$out = array();
$i = 0;

foreach( $data as $item) $out[$item['data']][$i++] = $item;

See working at IDEONE

Applied to code:

$sql = $this->db->query($query) or die (sprintf("Falha: %s", $this->db->error()));
$i = 0;
if ($sql->num_rows) {
  while ($row = $sql->fetch_object()) $data[$row['data']][$i++] = $row;
}

If you can reset the internal index, it becomes simpler:

$sql = $this->db->query($query) or die (sprintf("Falha: %s", $this->db->error()));
if ($sql->num_rows) {
  while ($row = $sql->fetch_object()) $data[$row['data']][] = $row;
}
    
13.01.2016 / 13:06
1

Assign the date as array key, like this:

while ($row = $sql->fetch_object())
    $data[$row['data']][] = $row;

If you do not have more than one line of code in the loop, you can remove the brackets. (brackets).

    
13.01.2016 / 13:00