I have a array with datetime objects .The array lookks like as follows
$advanceresult= array:68 [▼
"contact" => array:1 [▶]
"policyBranch" => ArrayCollection {#38322 ▶}
"assuranceContact" => ArrayCollection {#38337 ▶}
"info" => null
"withSurplusShare" => false
"withSurpassedSurplusShare" => false
"withoutPremiumInvoice" => false
"withoutPremiumInvoiceRange" => array:2 [▼
"start" => DateTime #1577833200 {#38339 ▶}
"end" => DateTime #1609369200 {#38346 ▶}
]
"showPoliciesFromArchivedContacts" => false
]
The withoutPremiumInvoiceRange key value is a datetime object.
My problem is when i encode this array as follows
$advanceresultencode=json_encode($advanceresult);
json_decode($advanceresultencode, true);
and decode it back the datetime objects is looking like as follows
enter code here
"withoutPremiumInvoiceRange" => array:2 [▼
"start" => array:3 [▼
"date" => "2020-01-01 00:00:00.000000"
"timezone_type" => 3
"timezone" => "Europe/Zurich"
]
"end" => array:3 [▶]
]
I need the data to datetime object.Can anyone help me acheiving this.
You can't have the datetime without an extra step. You can change the encode process (more info here: Change output of DateTime in json_encode) but same issue on json_decode.
Here is what you can do on json_decode :
<?php
// Here your json as string
$json = ...;
$jsonDecoded = json_decode($json);
$jsonDecoded['withoutPremiumInvoiceRange']['start'] = DateTime::createFromFormat(
'Y-m-d H:i:s.u',
$jsonDecoded['withoutPremiumInvoiceRange']['start']['date'],
new DateTimeZone($jsonDecoded['withoutPremiumInvoiceRange']['start']['timezone'])
);
Related
I have this array with other 16 arrays
array:16 [▼
0 => array:6 [▼
"id" => 1867
"meta_date" => Illuminate\Support\Carbon #1564536945 {#4600 ▼
date: 2019-07-31 01:35:45.0 UTC (+00:00)
}
"title" => ""
"summary" => """
1 => array:6 [▼
"id" => 1868
"meta_date" => Illuminate\Support\Carbon #1524215895 {#4465 ▼
date: 2018-04-20 09:18:15.0 UTC (+00:00)
}
"meta_type" => ""
"title" => ""
"summary" => """
and so on...
I want to sort the main array by date specified in "meta_date" field included on each "child" array, the sort can be $sort= "asc" or $sort= "desc"
and
I want to take a dynamic limit for the main array, for example if $limit=10 take the first 10 arrays sorted by date selected
currently, I'm using PHP 7.3 and laravel 8
Laravel's collections come with everything you need:
collect($arr)->{$sort === 'desc' ? 'sortByDesc' : 'sortBy'}('meta_date')->take($limit)->values()->all();
$assets = Asset::where('group_id', $request->group_id)->select('name')->get()->toArray();
dd($assets);
output:
array:2 [▼
0 => array:1 [▼
"name" => "wordpress"
]
1 => array:1 [▼
"name" => "joomla"
]
]
Error: Array to string conversion
$assets = Asset::where('group_id', $request->group_id)->select('name')->get()->toArray();
dd(implode(',', $assets));
..............................................................................................................................................
There is another way to do with collection pluck(),
$assets = Asset::where('group_id', $request->group_id)->get()->pluck('name')->implode(',');
Your output will be Wordpress,joomla
I've created a collection like this :
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:2 [▶]
]
1 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#661 ▶}
"foods" => array:2 [▶]
]
2 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#665 ▶}
"foods" => array:2 [▶]
]
]
}
I demand to achieve collection like this (with Laravel collection):
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:6 [▶]
]
]
}
Because orderId and orderCreated are all the same in arrays. I need to make single array that collect orderId and orderCreated with all foods.
Any suggestion?
You can take the data from the first one and just combine all of the food items. For instance like this:
$new = collect([
'orderId' => $old->first()->orderId,
'orderCreated' => $old->first()->orderCreated,
'foods' => $old->pluck('foods')->flatten(1),
]);
The exact implementation will depend on how you built your initial collection.
Trying to convert nested php array
$ar:3 [▼
"sotr" => array:5 [▼
0 => {#190 ▼
+"sId": "1"
+"sFIO": "Родион Романович Мишин"
+"sSalary": "7477.59"
}
1 => {#192 ▶}
2 => {#193 ▶}
3 => {#194 ▶}
4 => {#195 ▶}
]
"ticket" => array:5 [▶]
"task" => array:4 [▶]
]
into YAML string by using Symfony\Component\Yaml\Yaml in Laravel. So, when I'm using
$data = Yaml::dump($ar);
I see empty array like this:
sotr:\n
- null\n
- null\n
- null\n
- null\n
- null\n
How I can fix this?
solve it! Probably this solution not so smart, but it works.
Convert array into JSON string:
$data = json_decode(json_encode($ar), true);
Then using Symfony\Component\Yaml\Dumper class
$dumper = new Dumper();
$yaml = $dumper->dump($data);
And here the YAML formatted string:
sotr:
-
sId: '1'
sFIO: 'Родион Романович Мишин'
sSalary: '7477.59'
I want to use decoded JSON data as php objects to be able to used as follows:
return $data->title
however im running into a few errors. I am able to connect to the remote API url and get the requested data.
$api = 'https://remote.api.url/dataset/list';
$json = file_get_contents($api);
$data = json_decode($json, true);
dd($data);
When i die and dump the data i see the following:
array:1 [▼
"data" => array:5 [▼
0 => array:5 [▼
"id" => "qk4GtMb8"
"title" => "SSA's palliative care has an mHealth deficit "
"image" => "http://gstatic.acfee.org/akamaihd/i/52fdb957187"
"published_at" => "2016-06-10 08:05:00"
"created_at" => array:3 [▼
"date" => "2016-06-07 05:48:34.000000"
"timezone_type" => 3
"timezone" => "UTC"
]
]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
]
]
But i am unable to use the recieved data in object form. return $data->title;
i am pretty new to JSON any help would be appreciated.
Thanks in advance.
The second parameter of json_decode (which in your case is true) converts the objects to array. From official PHP docs: When TRUE, returned objects will be converted into associative arrays. So if you remove it you should be ok.