I have a base table where i store records per team in a json object;
[
{
"Id": 1,
"date": "01-01-2022",
"Group": "A",
"Description": "test 1"
},
{
"Id": 2,
"date": "01-01-2022",
"Group": "B",
"Description": "test 2"
},
{
"Id": 3,
"date": "08-01-2022",
"Group": "B",
"Description": "test 3"
},
{
"Id": 4,
"date": "16-01-2022",
"Group": "A",
"Description": "test 4"
},
{
"Id": 5,
"date": "16-01-2022",
"Group": "C",
"Description": "test 5"
}
]
Now for reporting purpose i need to pivot the table like:
Date
A
B
C
01-01-2022
test 1
test 2
-
08-01-2022
-
test 3
-
16-01-2022
test 4
-
test 5
I really dont know how to perform such action with PHP or Javascript.. Tried with for each loops but i only got the date as a unique value.
btw; for empty rows to show "-" is fine.
Related
I've a sample json data
"regions": [
{
"id": 1,
"name": "Region 1",
"state_id": 1,
"areas" :[ {
"id": 1,
"name": "area 1",
"region_id": 1},
{
"id": 2,
"name": "area 2",
"region_id": 1}
]
},
{
"id": 2,
"name": "Region 2",
"state_id": 1,
"areas" :[ {
"id": 3,
"name": "area 3",
"region_id": 2},
{
"id": 4,
"name": "area 4",
"region_id": 2}
]
}
]
How can I filter out the data the based on id in the regions? For example, if id is 2, then the response should be like
"regions": [
{
"id": 2,
"name": "Region 2",
"state_id": 1,
"areas" :[ {
"id": 3,
"name": "area 3",
"region_id": 2},
{
"id": 4,
"name": "area 4",
"region_id": 2}
]
}
]
You can json_decode that json string and then use array_filter method to filter regions
$regions = json_decode('{"regions":
[
{
"id": 1,
"name": "Region 1",
"state_id": 1,
"areas" :[ {
"id": 1,
"name": "area 1",
"region_id": 1
},{
"id": 2,
"name": "area 2",
"region_id": 1
}]
},{
"id": 2,
"name": "Region 2",
"state_id": 1,
"areas" :[{
"id": 3,
"name": "area 3",
"region_id": 2
},{
"id": 4,
"name": "area 4",
"region_id": 2
}]
}
]
}', true);
// Filter Region
$region_id = 2;
$filtered_regions = array_filter($regions['regions'], function($r) use ($region_id) {
return $r['id'] == $region_id;
});
// Filter By Area Id
$area_id = 2;
$filtered_areas = array_filter($regions['regions'], function($r) use ($area_id) {
$areas = array_filter($r['areas'], function($area) use ($area_id) {
return $area['id'] == $area_id;
});
return count($areas) > 0;
});
return ['regions' => $filtered_regions];
First apply json_decode on your json data then apply filter . If you want to get an whole object from this response array , then write a function which will accept the id and return that object after filtering.
(Edited to add a better example with multiple characters with the same book and author)
Using Laravel 8, but this must be a generic enough problem I just don't know the name for or can't find. I've tried "normalizing eloquent JSON response" ad nauseum and nothing seems to be this specific issue.
I have some basic relations defined in Laravel, but I want to separate them out for a JSON response.
e.g.
Character belongsTo Book
Book belongsTo Author
So (ignore any syntax issues, it's quickly typed pseudo code)
$characters = Character::with('book', 'book.author')->get();
$characters->toJson();
Output:
[
{
"id": 1,
"book_id": 1,
"name": "Some character",
"book": [{
"id": 1,
"author_id": 1,
"name": "Some book",
"author": [{
"id": 1,
"name": "Some author"
}]
}]
},
{
"id": 2,
"book_id": 1,
"name": "A second character",
"book": [{
"id": 1,
"author_id": 1,
"name": "Some book",
"author": [{
"id": 1,
"name": "Some author"
}]
}]
}
]
But what I really want is this so I can feed it into a front end (React) simple ORM, but unnested and deduplicated (since the originally nested response could have the same book entry for multiple characters for example).
{
"characters": [{
"id": 1,
"book_id": 1,
"name": "Some character"
}, {
"id": 2,
"book_id": 1,
"name": "A second character"
}],
"books": [{
"id": 1,
"author_id": 1,
"name": "Some book"
}],
"authors": [{
"id": 1,
"name": "Some author"
}],
}
I have been on a little project of mine now i want to find imdb_id using tmdb_id so for that I have been trying to use the API.
https://api.themoviedb.org/3/tv/67026?api_key=myapikey&append_to_response=external_ids
which brings up the results like this
{
"backdrop_path": "/hcFbIbDzsB9aSSw9VkSGFEl5sGO.jpg",
"created_by": [{
"id": 230174,
"credit_id": "577eb8e5c3a368694a0027ac",
"name": "David Guggenheim",
"gender": 2,
"profile_path": "/hqSydaadHO6EsBIn3BQEzzfxNUY.jpg"
}],
"episode_run_time": [42],
"first_air_date": "2016-09-21",
"genres": [{
"id": 18,
"name": "Drama"
}, {
"id": 10768,
"name": "War & Politics"
}],
"homepage": "https://www.netflix.com/title/80113647",
"id": 67026,
"in_production": false,
"languages": ["en"],
"last_air_date": "2019-06-07",
"last_episode_to_air": {
"air_date": "2019-06-07",
"episode_number": 10,
"id": 1809432,
"name": "#truthorconsequences",
"overview": "On election day, Kirkman turns to his therapist to assuage his conscience about the events -- and his own decisions -- of the momentous prior 36 hours.",
"production_code": "",
"season_number": 3,
"show_id": 67026,
"still_path": "/cpy3uV100RyZuvJN535JLTrj4Nz.jpg",
"vote_average": 7.0,
"vote_count": 1
},
"name": "Designated Survivor",
"next_episode_to_air": null,
"networks": [{
"name": "ABC",
"id": 2,
"logo_path": "/ndAvF4JLsliGreX87jAc9GdjmJY.png",
"origin_country": "US"
}, {
"name": "Netflix",
"id": 213,
"logo_path": "/wwemzKWzjKYJFfCeiB57q3r4Bcm.png",
"origin_country": ""
}],
"number_of_episodes": 53,
"number_of_seasons": 3,
"origin_country": ["US"],
"original_language": "en",
"original_name": "Designated Survivor",
"overview": "Tom Kirkman, a low-level cabinet member is suddenly appointed President of the United States after a catastrophic attack during the State of the Union kills everyone above him in the Presidential line of succession.",
"popularity": 30.031,
"poster_path": "/5R125JAIh1N38pzHp2dRsBpOVNY.jpg",
"production_companies": [{
"id": 28788,
"logo_path": null,
"name": "Genre Films",
"origin_country": "US"
}, {
"id": 19366,
"logo_path": "/vOH8dyQhLK01pg5fYkgiS31jlFm.png",
"name": "ABC Studios",
"origin_country": "US"
}, {
"id": 78984,
"logo_path": null,
"name": "Entertainment 360",
"origin_country": "US"
}],
"seasons": [{
"air_date": "2016-09-20",
"episode_count": 21,
"id": 78328,
"name": "Season 1",
"overview": "Tom Kirkman, a low-level cabinet member is suddenly appointed President of the United States after a catastrophic attack during the State of the Union kills everyone above him in the Presidential line of succession.",
"poster_path": "/1QHlD6z9FnXuuTDVLJnjrtLfVyq.jpg",
"season_number": 1
}, {
"air_date": "2017-09-27",
"episode_count": 22,
"id": 91130,
"name": "Season 2",
"overview": "",
"poster_path": "/z4hdj8cYyqCO9lVBOGm6YZsnMho.jpg",
"season_number": 2
}, {
"air_date": "2019-06-07",
"episode_count": 10,
"id": 122914,
"name": "Season 3",
"overview": "",
"poster_path": "/wn310FWQhjjpHbqsMRBcXr28EHc.jpg",
"season_number": 3
}],
"status": "Canceled",
"type": "Scripted",
"vote_average": 7.2,
"vote_count": 408,
"external_ids": {
"imdb_id": "tt5296406",
"freebase_mid": null,
"freebase_id": null,
"tvdb_id": 311876,
"tvrage_id": 51115,
"facebook_id": "DesignatedSurvivor",
"instagram_id": "designatedsurvivor",
"twitter_id": "DesignatedNFLX"
}
}
So now if I want to get just the IMDb_ID form the external_ids then what code should i use in PHP and store it in a variable.
Thank you very much in advance.
use the json_decode method to treat JSON like a PHP object
$data = 'your JSON object';//raw data as string in $data
$decoded = json_decode($data);//decoded data as PHP object
echo $decoded->external_ids->imdb_id; //access any property you like
hope this helps!!!
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I've got basic array from my api:
{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }
I want to get id value from request for my php variable lets say: $idVariable. How can I do it?
I tried something like:
$idVariable = $request->specialities[0]->id
but it seems not working. What is the right way?
Then how should I work with the arrays of object in this case:
{ "title": "offer title", "body": "offer body",
"specialities": [
{ "lang": "en", "id": "1", "icon": "0", "name": "speciality name 1" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 2" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 3" },
etc...], "region": "region1" }
To get id's of every object in specialities array? I know that it could be a duplicate question, but I ask for just a basic example.
I tried to use json decode like below:
json_decode($request->get('specialties'))->id
edit:
The almost-right way to do it is to decode json file first:
$data = json_decode($request);
and then get the right property from the array:
$id = $data['specialities'][0]['id'];
the problem now is that id is a string not an integer and by simply using:
$int_id = intval($id)
I've got $int_id = 0 instead of 1,2,3 etc
You are getting a response from API in JSON you should use json_decode() and then use the data. Try this code.
$json = '{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }';
$data = json_decode($json);
echo $data->specialities->id;
I've tried to switch the datasource of the Kendo UI Gantt example inside PHP. I have mapped the schema with what is being returned, but I just get a blank gantt chart with one heading - "undefined".
{
"1": {
"id": "1",
"orderId": "1",
"title": "TESTER1",
"start": "\/new Date('2016-01-01 09:00:00')\/",
"end": "\/new Date('2016-02-01 00:00:00')\/",
"project": "1",
"client": "4218",
"parent": "0",
"percentComplete": "10.11"
},
"2": {
"id": "2",
"orderId": "2",
"title": "TESTER2",
"start": "\/new Date('2016-01-03 09:00:00')\/",
"end": "\/new Date('2016-02-01 00:00:00')\/",
"project": "1",
"client": "4218",
"parent": "0",
"percentComplete": "50.00"
}
}
Above is the JSON being sent back to Kendo, but it doesn't render.
Found the solution:
I type casted the integers, set parents to null rather than zero (0) and converted dates into milliseconds in the PHP layer before putting through to Kendo. I also removed the keys which resulted in the below JSON to be created. This solved my rendering problem.
[{
"id": 1,
"orderId": 1,
"title": "TESTER1",
"start": "\/Date(1463126400000)\/",
"end": "\/Date(1463958000000)\/",
"project": 1,
"client": 4218,
"parent": null,
"percentComplete": 10
}, {
"id": 2,
"orderId": 2,
"title": "TESTER2",
"start": "\/Date(1463990400000)\/",
"end": "\/Date(1464130800000)\/",
"project": 1,
"client": 4218,
"parent": null,
"percentComplete": 50
}]