Create an array separated by date groups

Asked

Viewed 121 times

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

)

2 answers

3


Basically that’s all you need:

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

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

See working on IDEONE

Applied to the 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’s 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;
}
  • Thank you Bacco, I chose your solution for being simpler

  • @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

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

  • Blz @Bacco, I’ll do it ;)

1

Assign the date as the 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 repeat loop, you can remove the brakets. (square brackets).

  • Thank you Daniel, it worked perfectly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.