John Davidson

arrays - how to combine json data using php

0 comments
Message:


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


My Answer:
"January": {
"1": {
"name": "John",
"status": "Present"
},
"2": {
"name": "Jane",
"status": "Absent"
},
"3": {
"name": "Alice",
"status": "Present"
}
},
"February": {
"1": {
"name": "Bob",
"status": "Present"
},
"2": {
"name": "Mary",
"status": "Absent"
},
"3": {
"name": "Tom",
"status": "Present"
}
}
}

_now I want to combine the data from January and February into one array using PHP. Here's how you can do it:_

php

$json1 = '{
"January": {
"1": {
"name": "John",
"status": "Present"
},
"2": {
"name": "Jane",
"status": "Absent"
},
"3": {
"name": "Alice",
"status": "Present"
}
}
}';

$json2 = '{
"February": {
"1": {
"name": "Bob",
"status": "Present"
},
"2": {
"name": "Mary",
"status": "Absent"
},
"3": {
"name": "Tom",
"status": "Present"
}
}
}';

$data1 = json_decode($json1, true);
$data2 = json_decode($json2, true);

$combinedData = array_merge_recursive($data1, $data2);

print_r($combinedData);

?>


_This code will combine the data from January and February into one array called `$combinedData`. You can then use this combined data for further processing or display in your attendance report._

Rate this post

3 of 5 based on 9724 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us