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);
}
$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);
Related
I have an object like this, and I'm working with "todos" array, only want update active status with specific indexs are given:
{
"_id": "61e7e78372d3221d2c5fb242",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"todos": [
{
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 0
{
"name": "sub2",
"desc": "desc2",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 1
...
]
}
The first I try update with: ['$set' => ['todos.0.active' => false]]
{
"0": {
"active": false
},
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
},
active can be update to false, key 0 is generated. I continue try other:
array_push($pipeline, ['$set' => ['todos' => [
'$function' => [
'body' => 'function(todos) {
return todos; // or do something
}',
'args' => ['todos'],
'lang' => 'js',
]
]]]);
But todos field turn into string is "todos", not array. What wrong with $set and $function? And other operators can do the same easier like $map or $filter?
My full pipeline update:
[
{
"$set": {
"name": "First Task 1",
"desc": "This is description",
"status": "completed",
"done": 100,
"level": "medium",
"company": "BBB",
"project": "AAA",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"updated_at": "$$NOW"
}
},
{
"$set": {
"todos.1.active": false
}
},
{
"$set": {
"todos": {
"$concatArrays": [
"$todos",
[
{
"name": "sub3",
"desc": "desc3",
"active": true,
"owner": "61e6125db0f102060951aa53"
}
]
]
}
}
}
]
db.collection.updateOne(['_id' => ObjectId('')], $pipeline, ['multi' => true]})
I want to update with pipeline, not each single query.
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.$[elem].active": false
}
},
{
"multi": true,
"arrayFilters": [
{
"elem.name": {
$eq: "sub1"
}
}
]
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.0.active": false,
"todos.2.active": false
}
},
{
"multi": true
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
[
{
$set: {
todos: {
$map: {
input: {
$range: [ 0, { $size: "$todos" } ]
},
as: "i",
in: {
$let: {
vars: {
"item": { $arrayElemAt: [ "$todos", "$$i" ] }
},
in: {
$cond: {
if: {
$in: [ "$$i", [ 0, 2 ] ]
},
then: {
"name": "$$item.name",
"desc": "$$item.desc",
"active": false,
"owner": "$$item.owner"
},
else: "$$item"
}
}
}
}
}
}
}
}
],
{
"multi": true
})
mongoplayground
I have a json array with ids, datetimes and soldout attributes that looks like below:
[
{
"id": 2,
"date": "2020-06-07 09:30:00",
"fdate":"7 Jun",
"soldout": true
},
{
"id": 3,
"date": "2020-06-07 17:00:00",
"fdate":"7 Jun",
"soldout": false
},
{
"id": 4,
"date": "2020-06-08 10:00:00",
"fdate":"8 Jun",
"soldout": false
},
{
"id": 5,
"date": "2020-06-08 18:30:00",
"fdate":"8 Jun",
"soldout": true
}
]
I have a hard time to figure out to solve this problem.
I would like to group time(s), id and soldout attributes for a same date.
How do I convert the above json array to below json format in PHP:-
[
{
"date":"2020-06-07",
"fdate": "7 Jun",
"times": [
{
"id":2,
"time": "9:30 am",
"soldout: true
},
{
"id":3,
"time": "5:00 pm",
"soldout": false
}
]
},
{
"date":"2020-06-08",
"fdate": "8 Jun",
"times": [
{
"id":4,
"time": "10:00 am",
"soldout": false
},
{
"id":5,
"time": "6:30 pm",
"soldout": true
}
]
}
]
You can find my code here: https://rextester.com/KNGH26986
Try
$out = [];
foreach($x as $x)
{
$date = strtotime($x['date']);
$day = date('Y-m-d',$date);
$time = date('h:i a', $date);
if(!array_key_exists($x['fdate'],$out))
{
$out[$x['fdate']] = [
'fdate' => $x['fdate'],
'date' => $day,
];
$out[$x['fdate']]['times'][] = [
'id' => $x['id'],
'time' => $time,
'soldout' => $x['soldout']
];
}
else
{
$out[$x['fdate']]['times'][] = [
'id' => $x['id'],
'time' => $time,
'soldout' => $x['soldout']
];
}
}
$res=[];
foreach($out as $out)
{
$res[] = $out;
}
dd(json_encode($res));
Output will be
[
{
"date":"2020-06-07",
"fdate": "7 Jun",
"times": [
{
"id":2,
"time": "9:30 am",
"soldout": true
},
{
"id":3,
"time": "5:00 pm",
"soldout": false
}
]
},
{
"date":"2020-06-08",
"fdate": "8 Jun",
"times": [
{
"id":4,
"time": "10:00 am",
"soldout": false
},
{
"id":5,
"time": "6:30 pm",
"soldout": true
}
]
}
]
Thanks to #Jithesh Jose for the solution. I made few changes on his solution by including the source of the json input and making sure it can run on an online editor.
Here is the code :-
<?php
$json = '[{
"id": 2,
"date": "2020-06-07 09:30:00",
"fdate": "7 Jun",
"soldout": true
},
{
"id": 3,
"date": "2020-06-07 17:00:00",
"fdate": "7 Jun",
"soldout": false
},
{
"id": 4,
"date": "2020-06-08 10:00:00",
"fdate": "8 Jun",
"soldout": false
},
{
"id": 5,
"date": "2020-06-08 18:30:00",
"fdate": "8 Jun",
"soldout": true
}]';
$db = json_decode($json);
$out = array();
foreach( $db as $x ){
$date = strtotime($x->date);
$day = date('Y-m-d',$date);
$time = date('h:i a', $date);
if(!array_key_exists($x->fdate,$out))
{
$out[$x->fdate] = [
'fdate' => $x->fdate,
'date' => $day,
];
$out[$x->fdate]['times'][] = [
'id' => $x->id,
'time' => $time,
'soldout' => $x->soldout
];
}else
{
$out[$x->fdate]['times'][] = [
'id' => $x->id,
'time' => $time,
'soldout' => $x->soldout
];
}
}
$res=[];
foreach($out as $o)
{
$res[] = $o;
}
echo json_encode($res);
?>
I have this php code to return the Array from a JSON file how ever I am not able to return the objects (The Airport info structure) as well the Arrivals etc.
Apologies if this is not very clear I am fairly new to this.
Code:
$fxml_response = $client->request('GET', 'AirportBoards', ['query' => $queryParams]);
try {
$body = json_decode($fxml_response->getBody(), true);
if ($fxml_response->getStatusCode() == 200 && !array_key_exists('error', $body)) {
foreach (['arrivals', 'departures', 'enroute', 'scheduled',] as $board) {
if($body['AirportBoardsResult'][$board]) {
$boardFlights = $body['AirportBoardsResult'][$board]['flights'];
$response[$board] = $boardFlights;
}
}
} else {
$response['error'] = $body['error'];
}
} catch (Exception $e) {
echo json_encode(['error' => 'Failed to retrieve Airport Board details.']);
}
// Send back the data
header('Content-Type: application/json');
echo json_encode($response);
Json:
{
"AirportBoardsResult": {
"airport": "NZAP",
"airport_info": {
"airport_code": "NZAP",
"name": "Taupo",
"elevation": 1335.0,
"city": "Taupo",
"state": "",
"longitude": 176.084444,
"latitude": -38.739723,
"timezone": ":Pacific/Auckland",
"country_code": "NZ",
"wiki_url": "https://en.wikipedia.org/wiki/Taupo_Airport",
"alternate_ident": "TUO"
},
"arrivals": {
"num_flights": 6,
"next_offset": -1,
"flights": [{
"ident": "SDA806",
"faFlightID": "SDA806-1528092600-schedule-0000",
"airline": "SDA",
"airline_iata": "S8",
"flightnumber": "806",
"tailnumber": "ZK-PLV",
"type": "Form_Airline",
"blocked": false,
"diverted": false,
"cancelled": false,
"origin": {
"code": "NZWN",
"city": "Wellington",
"alternate_ident": "WLG",
"airport_name": "Wellington Int'l"
},
"destination": {
"code": "NZAP",
"city": "Taupo",
"alternate_ident": "TUO",
"airport_name": "Taupo"
},
"filed_ete": 3480,
"route": "KADNU1Q KAPTI WNAP2",
"filed_altitude": 210,
"display_filed_altitude": "21,000 feet",
"filed_airspeed_kts": 250,
"distance_filed": 191,
"filed_departure_time": {
"epoch": 1528265400,
"tz": "NZST",
"dow": "Wednesday",
"time": "06:10PM",
"date": "06/06/2018",
"localtime": 1528308600
},
"estimated_departure_time": {
"epoch": 1528265889,
"tz": "NZST",
"dow": "Wednesday",
"time": "06:18PM",
"date": "06/06/2018",
"localtime": 1528309089
},
"actual_departure_time": {
"epoch": 1528265889,
"tz": "NZST",
"dow": "Wednesday",
"time": "06:18PM",
"date": "06/06/2018",
"localtime": 1528309089
},
"departure_delay": 489,
"filed_arrival_time": {
"epoch": 1528268880,
"tz": "NZST",
"dow": "Wednesday",
"time": "07:08PM",
"date": "06/06/2018",
"localtime": 1528312080
},
"estimated_arrival_time": {
"epoch": 1528269502,
"tz": "NZST",
"dow": "Wednesday",
"time": "07:18PM",
"date": "06/06/2018",
"localtime": 1528312702
},
"actual_arrival_time": {
"epoch": 1528269180,
"tz": "NZST",
"dow": "Wednesday",
"time": "07:13PM",
"date": "06/06/2018",
"localtime": 1528312380
},
"arrival_delay": 300,
"status": "Arrived",
"progress_percent": 100,
"aircrafttype": "PC12",
"full_aircrafttype": "L/PC12",
"adhoc": false
}]
};
Add the airport info to the result after the loop.
if ($fxml_response->getStatusCode() == 200 && !array_key_exists('error', $body)) {
foreach (['arrivals', 'departures', 'enroute', 'scheduled',] as $board) {
if($body['AirportBoardsResult'][$board]) {
$boardFlights = $body['AirportBoardsResult'][$board]['flights'];
$response[$board] = $boardFlights;
}
}
$response['airport_info'] = $body['AirportBoardsResult']['airport_info'];
}
I have a table Commande which contains prestataire_id ,date ,heure ,... so I want to regroup the table by date. I tried this
$commande = $this->Commande->find('all', array('conditions' => array('Commande.prestataire_id' => $this->request->data['prestataire_id']),'order' => array('Commande.date' => 'DESC')));
the json response is like that
{
"status": "Success",
"status_code": 200,
"message": "Commande trouvé",
"data": {
"get_Mission": {
"data": [
{
"Commande": {
"id": "475",
"date": "2018-04-20",
"heure": "10:00:00",
"date_created": "2018-04-19",
"heure_created": "17:11:39",
"date_accepted": "2018-04-19",
"heure_accepted": "17:12:35",
"delai": "00:00:56",
"date_fin": "0000-00-00",
"heure_fin": "00:00:00",
},
},
{
"Commande": {
"id": "476",
"date": "2018-04-20",
"heure": "10:07:00",
"date_created": "2018-04-19",
"heure_created": "17:13:12",
"date_accepted": "0000-00-00",
"heure_accepted": "00:00:00",
"delai": "0",
"date_fin": "0000-00-00",
"heure_fin": "00:00:00",
},
},
{
"Commande": {
"id": "477",
"date": "2018-04-19",
"heure": "17:13:00",
"date_created": "2018-04-19",
"heure_created": "17:13:48",
"date_accepted": "2018-04-19",
"heure_accepted": "17:14:23",
"delai": "00:00:35",
"date_fin": "0000-00-00",
"heure_fin": "00:00:00",
},
}
]
}
}
}
But I want them to be regrouped by date like data ["2018-04-20"]-> {commande,commande} ,["2018-04-19"]->{"commande} any suggestion ?
What you could try is setting the month as your array key.
foreach($examples as $example){
$month = date('Y-m-d', strtotime($example['Model']['date']));
$exampleArray[$month][] = $example;
}
And re-use your $exampleArray after setting it.
I am trying to iterate both index of JSON(Players and Buildings), so that i can a get new result in jQuery
I have two index of JSON one having information of Players and second index having information of Building related Player.
I want to Parse it so that i can get Player and its building name.
My Actual JSON result
{
"Players": [
{
"id": "35",
"building_id": "8",
"room_num": "101",
},
{
"id": "36",
"building_id": "9",
"room_num": "102",
},
{
"id": "37",
"building_id": "10",
"room_num": "103",
},
{
"id": "38",
"building_id": "11",
"room_num": "104",
}
],
"Buildings": [
{
"id": "8",
"name": "ABC"
},
{
"id": "9",
"name": "DEF"
},
{
"id": "10",
"name": "GHI"
},
{
"id": "11",
"name": "JKL"
}
]
}
Need this
"information": [
{
"player_id": "35",
"Buildings_name": "ABC"
},
{
"player_id": "36",
"Buildings_name": "DEF"
},
{
"player_id": "37",
"Buildings_name": "GHI"
},
{
"player_id": "38",
"Buildings_name": "JKL"
}
]
}
Here you go, this go for each player and check if there are buildings and will map them to new structure. This will not filter values for buildings that do not have mapping to players, and will not include the buildings with no players.
var x = {
"Players": [
{
"id": "35",
"building_id": "8",
"room_num": "101",
},
{
"id": "36",
"building_id": "9",
"room_num": "102",
},
{
"id": "37",
"building_id": "10",
"room_num": "103",
},
{
"id": "38",
"building_id": "11",
"room_num": "104",
}
],
"Buildings": [
{
"id": "8",
"name": "ABC"
},
{
"id": "9",
"name": "DEF"
},
{
"id": "10",
"name": "GHI"
},
{
"id": "11",
"name": "JKL"
}
]
};
var res = $.map(x.Players, function(item) {
return {
player_id: item.id,
building_name: $.grep(x.Buildings, function(i) {
return i.id == item.building_id
}).length != 0 ? $.grep(x.Buildings, function(i) {
return i.id == item.building_id
})[0].name : undefined
}
})
and if you want to filter values that do not have relationships e.g INNER join
var resInnerJoin = $.grep($.map(x.Players, function(item) {
return {
player_id: item.id,
building_name: $.grep(x.Buildings, function(i) {
return i.id == item.building_id
}).length != 0 ? $.grep(x.Buildings, function(i) {
return i.id == item.building_id
})[0].name : undefined
}
}), function(it) {
return it.building_name != undefined
})
If you need it in PHP :
$json = '{...}';
// create and PHP array with you json data.
$array = json_decode($json, true);
// make an array with buildings informations and with building id as key
$buildings = array();
foreach( $array['Buildings'] as $b ) $buildings[$b['id']] = $b;
$informations = array();
for ( $i = 0 ; $i < count($array['Players']) ; $i++ )
{
$informations[$i] = array(
'player_id' => $array['Players'][$i]['id'],
'Buildings_name' => $buildings[$array['Players'][$i]['building_id']]['name']
);
}
$informations = json_encode($informations);
var_dump($informations);