0

I have an array of dates and values, for example:

$dates = ['2014-12-01', '2014-12-02', '2014-12-08', '2014-12-09', '2014-12-10', '2014-12-11'];
$values = [5, 3, 7, 8, 9, 2];

You'll note that 12/01 is a Monday, as is 12/08. I'd like to group data from these two arrays by day if the week:

[
    'monday' => [5, 7],
    'tuesday' => [3, 8],
    'wednesday' => [0, 9],
    'thursday' => [0, 2],
]

You'll note that the arrays are formed by grabbing the values associated with the days of the week. However, in the case that a Wednesday date exists, for example, but the prior Tuesday does not, then the array should have a "0". In other words, the 4 arrays should all be the same length.

NOTE: So far, I have only determined how to find the day of the week from a date: date('l', strtotime("2014-12-08")); I really can't figure out the general algorithm to solve this.

9
  • Have you made an attempt yet? It would be helpful to see it. Commented Dec 12, 2014 at 22:18
  • The explanation is unclear Commented Dec 12, 2014 at 22:21
  • A general algorithm: Iterate through the date array. For each date's key/value, determine the day of the week. Add the value for the same key from the values array to an array with a key that corresponds to the day of the week. Commented Dec 12, 2014 at 22:21
  • @showdev thanks a lot for your idea. I'm a little bit confused, however. Do you think you could write php code to do this? For example, what happens when a "Monday" is encountered and then the next date is also a "Monday". It seems more complicated than what you wrote. Commented Dec 12, 2014 at 22:23
  • 1
    You can append to the "week" array: $weekdays[$day_of_week][]=$values[$dates_key]; I'm sorry, I don't want to write all the code -- nothing personal. But the SO community will help to troubleshoot your code if you make an attempt and have trouble. Commented Dec 12, 2014 at 22:25

2 Answers 2

1
$dates  = array( '2014-12-01','2014-12-02','2014-12-08','2014-12-09',
                 '2014-12-10','2014-12-11' );
$values = array( 5, 3, 7, 8, 9, 2 );

$date  = strtotime(min($dates));
$stop  = strtotime(max($dates));
$dates = array_flip($dates);
$out   = array();

while($date <= $stop)
{
   $tmp = date('Y-m-d', $date);
   $out[date('l', $date)][] = isset($dates[$tmp]) && isset($values[$dates[$tmp]]) ?
                              $values[$dates[$tmp]] : 0;  
   $date = strtotime('+1 day', $date);   
}

print_r($out);

Result:

Array
(
    [Monday] => Array
        (
            [0] => 5
            [1] => 7
        )

    [Tuesday] => Array
        (
            [0] => 3
            [1] => 8
        )

    [Wednesday] => Array
        (
            [0] => 0
            [1] => 9
        )

    [Thursday] => Array
        (
            [0] => 0
            [1] => 2
        )

    [Friday] => Array
        (
            [0] => 0
        )

    [Saturday] => Array
        (
            [0] => 0
        )

    [Sunday] => Array
        (
            [0] => 0
        )

)

ps: how can I get the an array of all the dates included in the "dates" array associated with only all the Mondays?

Modify the code as, for example:

   $tmp = date('Y-m-d', $date);
   $exists = isset($dates[$tmp]) && isset($values[$dates[$tmp]]);
   $out[date('l', $date)]['numbers'][] = $exists ? $values[$dates[$tmp]] : 0; 
   if ($exists) $out[date('l', $date)]['dates'][] = $tmp;
   $date = strtotime('+1 day', $date);  

You'll get an output as (example for monday)

[Monday] => Array
    (
        [numbers] => Array
            (
                [0] => 5
                [1] => 7
            )

        [dates] => Array
            (
                [0] => 2014-12-01
                [1] => 2014-12-08
            )

    )
Sign up to request clarification or add additional context in comments.

6 Comments

@CodeGuy not sure what you are talking about. $monday = $out['Monday']; .. and so on?
thanks so much!! Sorry, I was trying to ask: how can I get the an array of all the dates included in the "dates" array associated with only all the Mondays?
great, thanks! One last thing: this assumes the dates are in order. What if the dates aren't in order such that the dates and associated indices should be sorted. How could I do that?
@CodeGuy My code does not care about order of the dates as long as $values and $dates correspond to each other. But if you really need it - stackoverflow.com/a/9917886/1164491
@CodeGuy give me example of data. It should not depend on the order. or are you looking at the values in [dates] => Array? Check the link above.
|
0

Might be a better way to get the 0s in there without another loop but I'm headed out:

foreach($dates as $key => $val) {
    $day = date('l', strtotime($val));
    $result[$day][] = $values[$key];
}
foreach($result as &$val) {
    if(count($val) == 1) {
        array_unshift($val, 0);
    }
}
print_r($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.