0

helo here I have JSON data. I made it in the helper based on the day of the month and year. here I make an attendance data report, and this is the JSON

{
    "data": {
        "user": {
            "id_user": "13",
            "name": "JHON"
        },
        "month": "01",
        "year": "2021",
        "attendace": [
            {
                "date": "01-01-2021",
                "in": "07:24:50",
                "out": "17:03:42"
            },
            {
                "date": "03-01-2021",
                "in": "07:08:50",
                "out": "17:18:50"
            },
            {
                "date": "07-01-2021",
                "in": "07:03:42",
                "out": "18:03:42"
            }
        ],
        "day": [
            {
                "day": "Friday",
                "date": "01-01-2021"
            },
            {
                "day": "Saturday",
                "date": "02-01-2021"
            },
            {
                "day": "Sunday",
                "date": "03-01-2021"
            },
            {
                "day": "Monday",
                "date": "04-01-2021"
            },
            ......
            ...... // to 
            {
                "day": "Sunday",
                "date": "31-01-2021"
            }
        ]
    }
}

the question is how to combine JSON absent with JSON day? and I want to make it like this, if the absent data is blank on date 5,6,7,8 then it is automatically null

I want to like this

 "day": [
            {
                "day": "Friday",
                "date": "01-01-2021" 
                "data" [
                  {
                    "in": "07:03:42",
                    "out": "18:03:42"
                   }
                ]
            },
            {
                "day": "Saturday",
                "date": "02-01-2021"
                "data" [
                  {
                   "in": null,
                   "out": null
                  }
                ]
            },
            {
                "day": "Sunday",
                "date": "03-01-2021"
                "data" [
                  {
                    "in": "07:03:42",
                    "out": "18:03:42"
                   }
                ]
            },
       ]

and this my code

pulic function get(){
    $data['user'] = $this->mymodel->find($id_user);
    $data['bulan'] = $bulan;
    $data['tahun'] = $tahun;
    $data['attendance'] = $this->mymodel->get_absen($id_user, $bulan, $tahun);
    $data['day'] = hari_bulan($bulan, $tahun);
    $message = array(
        'status'     => true,
        'data'       => $data
    );
    $this->response($message, REST_Controller::HTTP_OK);
}
1
  • What did you try so far? Commented Feb 13, 2021 at 20:47

1 Answer 1

1
$attendace = $data['attendace'];
$days = $data['day'];

$days = array_map(function($day) use ($attendace)  {
    $day['data'] = [];
    foreach ($attendace as $visit) {
        $day_date = new DateTime($day['date']);
        $visit_date = new DateTime($visit['date']);
        $interval = $day_date->diff($visit_date)->format('%d');
        if ($interval == 0) {
            $day['data'][] = [
                'in' => $visit['in'],
                'out'=> $visit['out']
            ];
        }
    }

    if (count($day['data']) === 0)
        $day['data'][] = [
            'in' => null,
            'out' => null
        ];

    return $day;
}, $days);
Sign up to request clarification or add additional context in comments.

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.