Sort multi-dimensional array - php

I have a multi dimensional array in the following form
array:2 [▼
"dashboardData" => array:1 [▶]
"widgetData" => array:5 [▼
0 => {#214 ▼
+"_id": "575fcf6d298fbfd833000041"
+"created": "2016-06-14T09:33:33.492Z"
+"dashboardid": "575fcebc298fbfd833000036"
+"datasource": {#215 ▶}
}
1 => {#249 ▶}
2 => {#285 ▶}
3 => {#297 ▶}
4 => {#333 ▶}
]
]
I have deleted a lot of data but the above just be enough to demonstrate what I am after. Essentially, the widgetData part of my array has a random order for its elements.
As you can see above though, each element in this part of my array has a created value. Is it possible to order just this part of my array (widgetData) based on the created date?
Thanks

Because your dates are using standard UTC date strings, you can sort your array by iterating the components and assigning the values to the original array (assuming it's variable), i.e:
let arrayCopy = array
for index in 0..<arrayCopy.count {
if let widgetData: [[String: AnyObject]] = array[index]["widgetData"] as? [[String: AnyObject]] {
let sortedWidgetData = widgetData.sort({ (dictionary1, dictionary2) -> Bool in
return (dictionary1["created"] as! String) < (dictionary2["created"] as! String)
})
array[index]["widgetData"] = sortedWidgetData
}
}
You could also just do this at runtime on your datasource as and when it's needed.

Related

How can I sort chosen collection in multidimensional array in laravel php

i want to sort by cu_customer_shortname in nested array collection but seems i cant see any ways do it . here is my example of my array list
{#1811 ▼
+"co_group_id": "ADUKO"
+"co_description": "ADUKO"
+"co_customer_id": "038"
+"co_active_sts": "1"
+"irel__h_d_rel_group_customer": array:3 [▼
0 => {#1812 ▼
+"ID": "5"
+"gc_group_id": "ADUKO"
+"gc_customer_id": "038"
+"irel__h_d_customer_master": {#1813 ▶}
}
1 => {#1814 ▼
+"ID": "6"
+"gc_group_id": "ADUKO"
+"gc_customer_id": "044"
+"irel__h_d_customer_master": {#1815 ▼
+"cu_customer_ID": "044"
+"cu_customer_Shortname": "JPEN" <------ Sort this shortname
}
}
]
Here is my code that i already tried
$groupList = $contents->data->groupList;
$order = $groupList->irel__h_d_rel_group_customer; //arrayList
$collection = new \Illuminate\Support\Collection($groupList); //collection
$sorted = $collection->SortByDesc('irel__h_d_customer_master.cu_customer_Shortname'); // sorted by cu_customer_shortname
My code not return any error but my list remain and not sorted by cu_customer_Shortname

Merge 2 Collections or Arrays by Key

I'm trying my best to prevent from looping over a collection to get more data that I need for a data table. I need a way to combine these 2 collections or arrays.
I have 2 functions inside a model that calls an API that gives me an array of data that I convert into a collection.
Orders()
public function orders(){
$orders_array = $this->api()->request('orders.json');
return collect($orders_array);
}
Returns:
Collection {#274 ▼
#items: array:2 [▼
0 => {#282 ▼
+"id": 628602961977
}
1 => {#270 ▶}
order_meta()
public function order_meta($order_id){
$order_metafields_array = $this->api()->request('meta.json');
return collect($order_metafields_array);
}
Returns:
Collection
#items: [
+"name": "meta_data"
]
What I am wanting to do is inject the order_meta() into each order using the order ID.
I guess I am wanting to do something like this:
public function orders_detailed(){
$orders = $this->orders();
$keyed = $orders->mapWithKeys(function ($item) {
return [$item['meta'] => $this->order_metafields($item['id'])];
});
}
In hopes it will return:
Collection {#274 ▼
#items: array:2 [▼
0 => {#282 ▼
+"id": 628602961977
+meta => {
+"name":"meta_data"
}
}
1 => {#270 ▶}
Is something like this possible? I'm also open to merging it in as an array first, then creating a collection.

PHP, array of objects, how can I use one set of object values as the array keys [duplicate]

This question already has answers here:
Get two columns of data as an array of keys and values using Laravel
(2 answers)
Closed 7 months ago.
I have an array that looks like this:
array:9 [▼
0 => {#279 ▼
+"id": "103"
+"name": "In what city did you meet your spouse/partner?"
}
1 => {#280 ▼
+"id": "100"
+"name": "What is the first name of the person you first kissed?"
}
2 => {#281 ▼
+"id": "102"
+"name": "What is the name of your favorite childhood friend?"
}
3 => {#282 ▶}
4 => {#283 ▶}
5 => {#284 ▶}
6 => {#285 ▶}
7 => {#286 ▶}
8 => {#287 ▶}
]
This is the dd(). I'm wondering how it might be possible to transform it into a key/value array, using the id as the key and the name as the value. Something like this:
array(
'103' => 'In what city did you meet your spouse/partner?',
'100' => 'What is the first name of the person you first kissed?'
);
As of PHP 7, you can use array_column() on objects, with the third parameter as the column to index by...
$questions = array_column($data, "name", "id");
You can use a collections pluck method:
$collection = collect($array)->pluck("name","id");
If you want to get an array back use:
$collection->all();
I figured it out with:
$data = DB::connection()->select($sql);
$questions = [];
foreach ($data as $question) {
$questions[$question->id] = $question->name;
}
Open to any slicker solutions, but this definitely works!

Laravel collection pluck dropping a value

I have the following Eloquent query in a Laravel 5.2 project:
$regsByCtryCollection = Organisation::join('countries_currencies', 'countries_currencies.id', '=', 'organisations.country_id')
->select(DB::raw('DISTINCT LCASE(countries_currencies.country_code) AS ctry, COUNT(organisations.id) AS val'))
->groupBy('ctry')
->get();
The raw query produces this output:
ctry val
at 1
au 5
br 1
The Eloquent call produces a collection of three rows (matching raw query output) like this:
Collection {#791 ▼
#items: array:3 [▼
0 => Organisation {#777 ▼
#table: "organisations"
#hidden: []
........
#attributes: array:2 [▶]
#original: array:2 [▼
"ctry" => "at"
"val" => 1
]
#relations: array:5 [▶]
........
}
1 => Organisation {#778 ▶}
2 => Organisation {#779 ▶}
]
}
I then pluck the values and format for Highmaps like this
$regsByCtry = $regsByCtryCollection->pluck('ctry', 'val')->map(function($country, $value) {
return [
"hc-key" => $country,
"value" => $value
];
})->values()->toJson();
And one of the values is dropped and I get this:
[
{"hc-key":"br","value":1},
{"hc-key":"au","value":5}
]
Why is the first entry getting dropped?
{"hc-key":"at","value":1}
I am using this same process with two other Eloquent queries and it works as expected, but just not on this collection.
Additionally, I also sum all the values in the array of objects like this:
$regsTotal = array_sum($regsByCtryCollection->pluck('val')->toArray());
And I get the correct value, including all three records summed:
$regsTotal = 7;
The issue is with pluck('ctry', 'val'). This will return val as key & ctry as value. In your query output at & br has same value 1. So one of it getting replaced by the other one.
Try pluck('val', 'ctry')->map(function($value, $country)
Reference

Yaml nested arrays in php

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'

Categories