1

Here below is my code.

I'm trying to plot graph values using the below array. On the dates array i have all 30 days in the array. I have another array called pending_date and approved_date. I need to match the date.Add the key and value for the matching key(date) array. Can u tell me where I'm going wrong ?

$approved_date = array('2017-09-01'=>'1','2017-09-02' => '2', '2017-09-03' => '4');
$pending_date = array('2017-09-01'=>'2');
$rejected_date = array();

Tried Code

        $myYearMonth = date('Y-m');
        $start = new DateTime(date('Y-m-01', strtotime($myYearMonth)));
        $end = new DateTime(date('Y-m-t', strtotime($myYearMonth)).' +1 day');
        $diff = DateInterval::createFromDateString('1 day');
        $periodStart = new DatePeriod($start, $diff, $end);

        foreach ( $periodStart as $dayDate ){
         $dates[]['period'] = $dayDate->format( "Y-m-d" );
        }

        foreach( $pending_date as $key => $value ){
          $data_key = array_search( $key, $dates );
          if( $data_key !== false ) $dates[ $data_key ] = $value;
        }

Expected Output

{"period": "2017-09-01", "approved": 1, "pending": 2, "rejected": 0},
{"period": "2017-09-02", "approved": 2, "pending": 0, "rejected": 0},
{"period": "2017-09-03", "approved": 4, "pending": 0, "rejected": 0}
3
  • You've provided expected output, but what's your current output? Commented Sep 18, 2017 at 10:48
  • I'm confused on the pushing array on particular array value.. Commented Sep 18, 2017 at 10:51
  • Then how can i acheive with the expected output ? "period": "2017-09-01" Commented Sep 18, 2017 at 10:52

1 Answer 1

3

Try something like this:

$approved_date = array('2017-09-01'=>'1','2017-09-02' => '2', '2017-09-03' => '4');
$pending_date = array('2017-09-01'=>'2');
$rejected_date = array();

$dates = array();
//$periods = array_unique(array_merge(array_keys($approved_date), array_keys($pending_date), array_keys($rejected_date)));
$periods = array(); // fill $periods how you like
for($i=1; $i<=30; $i++) {
    $periods[] = '2017-09-' . str_pad($i, 2, '0', STR_PAD_LEFT);
}
foreach ($periods as $period) {
    $dates[] = array(
        'period' => $period,
        'approved' => isset($approved_date[$period]) ? $approved_date[$period] : 0,
        'pending' => isset($pending_date[$period]) ? $pending_date[$period] : 0,
        'rejected' => isset($rejected_date[$period]) ? $rejected_date[$period] : 0,
    );
}

echo json_encode($dates);
Sign up to request clarification or add additional context in comments.

4 Comments

almost done.. But the thing is. i need all 30 days in this array. how can i do that ?
You wrote "On the dates array i have all 30 days in the array."... So you need to get all 30 days?
shall i add this one "foreach ( $periodStart as $dayDate ){ $dates[] = $dayDate->format( "Y-m-d" ); }"?
You can fill $periods how you like. I updated code with example. Now it generate 30 days.

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.