How to group dates from an array?

Asked

Viewed 233 times

3

I’ve got a job to do here and I don’t even know where to start:

I have an array of dates to group:

 array (size=12)<br>
      0 => string '2016-08-22' (length=10)<br>
      1 => string '2016-08-23' (length=10)<br>
      2 => string '2016-08-24' (length=10)<br>
      3 => string '2016-08-29' (length=10)<br>
      4 => string '2016-09-05' (length=10)<br>
      5 => string '2016-09-12' (length=10)<br>
      6 => string '2016-09-19' (length=10)<br>
      7 => string '2016-09-20' (length=10)<br>
      8 => string '2016-09-21' (length=10)<br>
      9 => string '2016-09-26' (length=10)<br>
      10 => string '2016-10-03' (length=10)<br>
      11 => string '2016-10-10' (length=10)<br>

I need to divide this array into several arrays organized by month and year, ex:

'datas' =><br>
array(size=3)<br>
  -- 0 => array(size=4)<br>
  ------ 0 => string '2016-08-22' (length=10)<br>
  ------ 1 => string '2016-08-23' (length=10)<br>
  ------ 2 => string '2016-08-24' (length=10)<br>
  ------ 3 => string '2016-08-29' (length=10)<br>
  -- 1 => array(size=6)<br>
  ------ 0 => string '2016-09-05' (length=10)<br>
  ------ 1 => string '2016-09-12' (length=10)<br>
  ------ 2 => string '2016-09-19' (length=10)<br>
  ------ 3 => string '2016-09-20' (length=10)<br>
  ------ 4 => string '2016-09-21' (length=10)<br>
  ------ 5 => string '2016-09-26' (length=10)<br>     
  -- 2 => array(size=2)<br>
  ------ 0 => string '2016-10-03' (length=10)<br>
  ------ 1 => string '2016-10-10' (length=10)<br>

I will use these arrays to generate calendars in a function that is already ready, just group the dates in arrays, which will return me the calendar of each month with the dates marked.

1 answer

4


Can make each month/year a key of the new array:

<?php
$datas = array(
    '2016-09-25',
    '2016-09-08',
    '2016-10-11',
    '2016-10-05',
    '2016-11-19',
    '2016-11-05',
    '2016-09-07',
);

$novasDatas = array();
foreach($datas as $data) {
    $mesAno = explode('-', $data);
    $novasDatas[$mesAno[0]. '-' .$mesAno[1]][] = $data;
}

echo '<pre>', print_r($novasDatas), '</pre>';

Upshot:

Array
(
    [2016-09] => Array
        (
            [0] => 2016-09-25
            [1] => 2016-09-08
            [2] => 2016-09-07
        )

    [2016-10] => Array
        (
            [0] => 2016-10-11
            [1] => 2016-10-05
        )

    [2016-11] => Array
        (
            [0] => 2016-11-19
            [1] => 2016-11-05
        )

)

In case you do not want each key to be the year/month, but rather numerical Keys do:

$datas = array(
    '2016-09-25',
    '2016-09-08',
    '2016-10-11',
    '2016-10-05',
    '2016-11-19',
    '2016-11-05',
    '2016-09-07',
);

$novasDatas = array();
$temp = array();
foreach($datas as $data) {
    $mesAno = explode('-', $data);
    $mesAno = $mesAno[0]. '-' .$mesAno[1];
    if(!in_array($mesAno, $temp)) {
        $temp[] = $mesAno;
    }
    $novasDatas[array_search($mesAno, $temp)][] = $data;
}

echo '<pre>', print_r($novasDatas), '</pre>';

Upshot:

Array
(
    [0] => Array
        (
            [0] => 2016-09-25
            [1] => 2016-09-08
            [2] => 2016-09-07
        )

    [1] => Array
        (
            [0] => 2016-10-11
            [1] => 2016-10-05
        )

    [2] => Array
        (
            [0] => 2016-11-19
            [1] => 2016-11-05
        )

)
  • That’s exactly it, I’m even embarrassed to not reach this conclusion, I was trying to make the Lice but it wasn’t working very well!

  • Glad I could help

Browser other questions tagged

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