Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
My present objects
{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Public",
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public"
}
}
I want the grouped object like
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
}
},
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-0",
"date": "January 05, 2021",
"type": "Private"
},
}
}
Unfortunately, the input data you provided is not valid JSON. There were commas missing or at the wrong place and the outer curly brackets should have been square ones. Here is a "corrected" version and a way to produce the output you desired as "closely as possible":
$in='[
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-04 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Public"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Public"
}
]';
$out=array_reduce(json_decode($in), function($a,$c){
$a[substr($c->cd,0,10)][$c->type][]=[
"_id"=>$c->_id,
"user_id"=>$c->user_id,
"account_status"=>$c->account_status,
"compare_date"=>$c->compare_date,
"date"=>$c->date,
"type"=>$c->type ];
return $a;}, [] );
array_walk($out,function($arr,$key) use(&$res) {
$arr['Date']=$key;
array_walk($arr, function($ar,$k) use($key,&$res) {
$res[$key][$k]=$ar;
});
});
foreach ($res as $el) $result[]=$el;
print_r($result);
You can find a working demo here: https://rextester.com/BQWU57249
This is the output:
array
(
[0] => Array
(
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-04
)
[1] => Array
(
[Private] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Private
)
[1] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Private
)
)
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-05
)
)
Assuming you want them grouped by date, and then further by the type of the account, you could do something like this.
A relatively simple foreach loop should do the trick, considering you have transformed your json into a array of arrays.
$filteredAccounts = [];
foreach ($accounts as $account) {
$date = new \DateTime($account['date']);
$filteredAccounts[$date->format('Y-m-d')][$account['type']][] = $account;
}
// Then you build your final array you want.
$groupedAccounts = [];
foreach ($filteredAccounts as $date => $filteredAccount) {
$filteredAccount['Date'] = $date;
$groupedAccounts[] = $filteredAccount;
}
var_dump($groupedAccounts);
But as others have mentioned your question doesn't really have anything to do with Laravel, but regardless this should help you along the way if you wanted it in a raw way.
Related
I have a Laravel collection grouped by day of the week but they are in the wrong order. I need them ordered by day of the week starting with Monday.
My Controller:
public function getWeeklySchedule()
{
$testSchedule = VolunteerSchedule::all();
$result = $testSchedule->groupBy(['assignment', function ($item) {
return $item['scheduled_days'];
}]);
return $result;
}
The result looks like this:
"Wednesday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 4,
"full_name": "Martine Kemmer Sr.",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Thursday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 7,
"full_name": "Leonardo Schaefer V",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 5,
"full_name": "Prof. Pablo Corwin",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 6,
"full_name": "Samantha Feil",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Friday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 9,
"full_name": "Miss Eleonore Kutch IV",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 4,
"full_name": "Martine Kemmer Sr.",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Tuesday": {
"kitchen": [
{
"id": 2,
"full_name": "Miss Lessie Torphy",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 7,
"full_name": "Leonardo Schaefer V",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Saturday": {...
Some fields have been removed for brevity. The days of the week are stored as a serialized array in the database. What is the best way to accomplish this?
Just define a custom sort function that uses a map for the days of the week:
$days = ["Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6, "Sunday" => 7];
$collection = collect(["Friday" => [], "Wednesday" => [], "Tuesday" => []]);
$collection->sortBy(fn($val, $key) => $days[$key]);
dump($collection);
Output:
=> Illuminate\Support\Collection {#4977
all: [
"Tuesday" => [],
"Wednesday" => [],
"Friday" => [],
],
}
So your code becomes:
public function getWeeklySchedule(): Collection
{
$days = ["Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6, "Sunday" => 7];
return VolunteerSchedule::all()
->groupBy(['assignment', fn ($item) => $item['scheduled_days']])
->sortBy(fn ($val, $key) => $days[$key]);
}
It will 100% work. It will sort collection from monday to sunday.
$ProductAvailabilities =ProductProductAvailability:get();
$days = [
'monday'=>1,
'tuesday'=>2,
'wednesday'=>3,
'thursday'=>4,
'friday'=>5,
'saturday'=>6,
'sunday'=>7
];
$sortingDays = array();
foreach($days as $day => $value)
{
$sortingDays[] = $ProductAvailabilities->where('day_availability',$day);
}
$ProductAvailabilities=collect($sortingDays)->flatten();
I have some problems with Eloquent Eager Loading. I have added whereHas to remove the blog don't meet the comment criteria but the comment stills return empty array. My intention is to completely remove it from the json record.
How can I completely remove the json data chain that does not meet my condition?
My current code:
User::select("id", "name", "email")
->with(['blog', 'blog.author', 'blog.comments' => function ($query) {
$query->where('comment', 'John is here');
}, 'blog.comments.owner'])
->whereHas('blog.comments', function ($query) {
$query->where('comment', 'John is Here');
})
->get();
My current json output is:
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"blog": [
{
"id": 1,
"created_at": "2021-04-09T18:08:06.000000Z",
"updated_at": "2021-04-09T10:33:03.000000Z",
"title": "First Blog",
"description": "Awesome",
"users_id": 1,
"cover": null,
"author": {
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0
},
"comments": [
{
"id": 1,
"comment": "John is here",
"blog_id": 1,
"user_id": 1,
"created_at": null,
"updated_at": null,
"owner": {
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0
}
}
]
},
{
"id": 6,
"created_at": "2021-04-12T07:41:43.000000Z",
"updated_at": "2021-04-12T08:01:18.000000Z",
"title": "Second Blog",
"description": "Awesome",
"users_id": 1,
"cover": "images/json_1618213303.png",
"author": {
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0
},
"comments": []
}
]
}
My expected output would be:
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"blog": [
{
"id": 1,
"created_at": "2021-04-09T18:08:06.000000Z",
"updated_at": "2021-04-09T10:33:03.000000Z",
"title": "First Blog",
"description": "Awesome",
"users_id": 1,
"cover": null,
"author": {
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0
},
"comments": [
{
"id": 1,
"comment": "John is here",
"blog_id": 1,
"user_id": 1,
"created_at": null,
"updated_at": null,
"owner": {
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0
}
}
]
}
]
}
Try this,
User::select("id", "name", "email")
->with([
'blog' => function ($query) {
$query->whereHas('comments', function ($query) {
$query->where('comment', 'John is Here');
});
},
'blog.comments' => function ($query) {
$query->where('comment', 'John is here');
},
'blog.author',
'blog.comments.owner'])
->get();
You should be applying the constraint on blog eager loading as well. Your query only filters comments of a blog
I am working on restfull api's for mobile app development. I am returning an php array in json which is like,
"data": {
"Waxing": [
{
"id": "119",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "15",
"date": "09-06-2018",
"time": "08.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "D",
"deleted_at": null,
"created_at": "2018-06-09 05:22:47",
"updated_at": "2018-06-09 14:22:04",
"setdate": "09 Jun",
"name": "Waxing"
}
],
"Massage": [
{
"id": "145",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "4",
"date": "14-06-2018",
"time": "08.00 AM,09.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "O",
"deleted_at": null,
"created_at": "2018-06-13 14:24:15",
"updated_at": "2018-06-13 14:24:15",
"setdate": "14 Jun",
"name": "Massage"
}
]
},
and creating this output with these line of code:
foreach ($getproviderdatetime as $getproviderdatetimes){
$categorycheck = Category::where('id', '=', $getproviderdatetimes->category_id)->first();
if(count($categorycheck)>0){
$alldata[$categorycheck->name][]=$getproviderdatetimes;
}
}
but now I need data in this format:
"data": {
[
"categoryname": "Waxing",
"services": [
{
"id": "119",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "15",
"date": "09-06-2018",
"time": "08.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "D",
"deleted_at": null,
"created_at": "2018-06-09 05:22:47",
"updated_at": "2018-06-09 14:22:04",
"setdate": "09 Jun",
"name": "Waxing"
}
]
],
[
"categoryname": "massage",
"services": [
{
"id": "145",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "4",
"date": "14-06-2018",
"time": "08.00 AM,09.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "O",
"deleted_at": null,
"created_at": "2018-06-13 14:24:15",
"updated_at": "2018-06-13 14:24:15",
"setdate": "14 Jun",
"name": "Massage"
}
]
]
},
I have array of services and getting category name from database. I am using Laravel framework and returning data in json format. How can I achive this. Need help.
Here's the solution:
foreach ($getproviderdatetime as $getproviderdatetimes){
$categorycheck = Category::where('id', '=', $getproviderdatetimes->category_id)->first();
if(count($categorycheck)>0){
// append new item to all data:
$alldata[] = [
"categoryname" => $categorycheck->name,
"services" => [$getproviderdatetimes],
];
}
}
$testing = []
foreach ($alldata as $key=>$all){
array_push($testing,["categoryname"=>$key,"services"=>$all]);
}
print_r($testing);
Loop again ur $alldata..Bellow is ur solution
https://3v4l.org/9iT9l
I got a some trouble in my Laravel application, in Search function, when I do search the result is
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "4232343432",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
{
"id": 3,
"user_id": 16,
"identity_number": "213918273",
"name": "Pekerja_3",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2345234234",
"image": null,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"partner_id": null,
"skill": []
}
]
}
as you can see that "Skill" with id number 3 is empty, I want all in id number 3 is empty also.
My controller is :
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])->whereHas('skill');
And I want something like this :
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2534234234",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
]
}
The point is, if "skill" variable is empty then data not show and vice versa.
Thankyou any help will appreciate, sorry for bad english
You can add a callback to your whereHas function with the same filter as the with function
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])
->whereHas('skill', function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
})
->get();
You can try this $worker = Worker:has('skill')->get();
Only worker that have at least one skill are contained in the collection
I have a feed from a parcel tracking service that i am trying to integrate into my site. I have a url which allows me to put the tracking number at the end and get a json response. I have multiple objects from this which include some static info such as the senders address and some info which I will need to use a foreach for like the tracking progress.
I believe I have got the string okay however I am not sure how I am meant to display the info.
This is what I have so far:
Example URL:
domain.com/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665
URL Returns:
{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}
Current PHP:
//set tracking url
$url = "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665";
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//call api
//$json = file_get_contents($url);
$json = json_decode($output);
$Address1 = $json->results[0]->Consignee->Address1;
$Address2 = $json->results[0]->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
Your json string doesn't have any results key (so I'm not sure why you trying to access results[0].
You can just use
$Address1 = $json->Consignee->Address1;
$Address2 = $json->Consignee->Address2;
Check this code:
$s = <<< END
{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}
END;
$json = json_decode($s);
$Address1 = $json->Consignee->Address1;
$Address2 = $json->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
Here is the output:
Address 1: 25 HEATHFIELD ROAD, Address 2: SHOLING
Below is json string in $json variable and access with RecursiveIteratorIterator
Example
$json='{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}';
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n</br>";
} else {
echo "$key => $val\n";
}
}
OUTPUT:
Agent => Consignee:
Address1 => 25 HEATHFIELD ROAD Address2 => SHOLING Address3 => Code => Company => ERIK HANSON Country => GREAT BRITAIN Dept => Email => ERIK.HANSON66#GOOGLEMAIL.COM Name => Phone => 07770320490 Postcode => SO19 1DL State => HANTS Town => SOUTHAMPTON CrossIdx => Error => NonDel => POD:
Date => 13 Jul 2016 Status => Harnett Time => 09:17 Pieces => 1 PosErr => 0 Tracks:
0:
Date => 13 Jul 2016 Status => Out for delivery Time => 07:10 1:
Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:24 2:
Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:22 3:
Date => 12 Jul 2016 Status => Arrived At Ryton Time => 22:12 4:
Date => 12 Jul 2016 Status => Preparing for despatch Time => 14:00 5:
Date => 12 Jul 2016 Status => Scanned into OCS HEATHROW LONDON Time => 13:59 6:
Date => 12 Jul 2016 Status => Consignment details verified Time => 13:59 7:
Date => 14 Jun 2016 Status => Shipment prepared by customer Time => 11:20 8:
Date => 02 Jan 2003 Status => Collected from Customer Time => 09:32 Weight => 7