'How to efficiently map between an existing array and a result of query Collection?
I have an array containing several objects with keys initialized to 0 except for the first key in each object which is initialized to certain date:
$data = [];
for ($i=0; $i<4; $i++) {
$data[] = (object)['main_key' => DateTime formatted string, 'key1' => '0', 'key2' => '0'];
}
I also have a query that groups by main_key and returns a result:
Illuminate\Support\Collection {
#items: array:4 [
0 => {
+"main_key": 2022-05
+"key1": "some_value"
+"key2": "some_value"
}
1 => {
+"date": 2022-05
+"key1": "some_value"
+"key2": "some_value"
}
2 => {
+"date": 2022-04
+"key1": "some_value"
+"key2": "some_value"
}
3 => {
+"date": 2022-03
+"key1": "some_value"
+"key2": "some_value"
}
]
How can I efficiently map the values so that the values of key1 and key2 will be set in the correct date? (They have the same number of objects)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
