How to parse a laravel collection - php

I have a laravel collection on output, I want to parse it ->toArray()
Collection {#335
#items: array:2 [
"0f39b1e0-a507-11e7-9d6e-33e84951047e" => array:2 [
"total_amount" => 25000
"debt_type" => array:2 [
0 => "car_loan"
1 => "car_loan"
]
]
"0f218520-a507-11e7-b0ba-8554a4ad039b" => array:2 [
"total_amount" => 15000
"debt_type" => array:1 [
0 => "house_loan"
]
]
]
}
is there any way to parse it so I get the following output:
array:1[
0=>[
'debt_id'=>'0f39b1e0-a507-11e7-9d6e-33e84951047e',
'debt_type'=>'car_loan',
'total_amount'=>25000
],
1=>[
'debt_id'=>'0f218520-a507-11e7-b0ba-8554a4ad039b',
'debt_type'=>'house_loan',
'total_amount'=>15000
]
]
what I have tried it works but not sure if its a good way to go around it:
$appDebts = $appDebts->groupBy('debt_type_id')->map(function ($item) {
return [
'total_amount' => $item->sum('amount'),
'debt_type' => $item->map(function ($item) {
return $item->debt_type->slug;
})->toArray(),
];
})->toArray();
if you dd $appDebts you get the collection that I have added on top of the post
$carLoan = [];
$studentLoan = [];
$houseLoan = [];
$cardLoan = [];
foreach ($appDebts as $debt) {
if ($debt['debt_type'][0] === 'car_loan') {
$carLoan['TotalAmount'] = $debt['total_amount'];
$carLoan['LoanType'] = $debt['debt_type'][0];
}
if ($debt['debt_type'][0] === 'house_loan') {
$houseLoan['TotalAmount'] = $debt['total_amount'];
$houseLoan['LoanType'] = $debt['debt_type'][0];
}
if ($debt['debt_type'][0] === 'student_loan') {
$studentLoan['TotalAmount'] = $debt['total_amount'];
$studentLoan['LoanType'] = $debt['debt_type'][0];
}
if ($debt['debt_type'][0] === 'credit_card_loan') {
$cardLoan['TotalAmount'] = $debt['total_amount'];
$cardLoan['LoanType'] = $debt['debt_type'][0];
}
}

Based on the array you shared:
$parsed = $collection->map(function ($item, $id) {
return [
'debt_id' => $id,
'debt_type' => collect($item['debt_type'])->first(),
'total_amount' => $item['total_amount']
];
})->values()->toArray();
With values you remove the key => value, you get the array without keys

Try with this mapping after the first one that you did :
$appDebts = $appDebts->groupBy('debt_type_id')->map(function ($item) {
return [
'total_amount' => $item->sum('amount'),
'debt_type' => $item->map(function ($item) {
return $item->debt_type->slug;
})->toArray(),
];
}); // <-- remove ->toArray() from here
$appDebts = $appDebts->map(function ($item, $key) {
return [
'debt_type_id' => $key
'debt_type' => $item["debt_type"][0], // assuming you want the first type !!
'total_amount' => $item["total_amount"],
];
})->toArray();
PS : This convert the given collection to tha wanted array for more performance tweaking consider editing the SQL query or the logic of getting appDebts

The only thing I can add to #Llopele's answer is to use keyBy() for easier data access:
$parsed = $collection->map(function ($item, $id) {
return [
'debt_id' => $id,
'debt_type' => collect($item['debt_type'])->first(),
'total_amount' => $item['total_amount']
];
})->values()->keyBy('debt_type')->toArray();
So now you can access data like this Arr::get($parsed, 'house_loan');

Related

Laravel group by change keys

Hi I have reservations and their start times. Now I need to raport it.
$monthlyAmounts = Reservation::all()
->groupBy(function ($proj) {
return cdate($proj->start_time)->format('Y-m');
})
->map(function ($month) {
return $this->sumTime($month);
});
sumTime is my function to calculate diff between start time and end time.
cdate is helper -> carbon::parse($date)..
Result of this is:
array:2 [▼
"2020-01" => 60
"2020-02" => 420
]
Need
But for API it would be better to get like this:
[
"2020" => [
'01' => 60,
'02' => 30
],
"2021" => [
'01' => 30,
]
]
My try
Unfortunately I can't make it like that. I tried:
$monthlyAmounts = Reservation::all()
->groupBy(function ($proj) {
return cdate($proj->start_time)->format('Y-m');
})
->map(function ($month) {
return $this->sumTime($month);
})
->mapToGroups(function ($item,$key) {
$arrkey = explode('-',$key);
return [$arrkey[0]=>[$arrkey[1]=>$item]];
});
But It makes this:
array:1 [▼
2020 => array:2 [▼
0 => array:1 [▼
"01" => 60
]
1 => array:1 [▼
"02" => 420
]
]
]
So I can't do $res['2020']['01']. How to do It?
Try this:
$monthlyAmounts = Reservation::all()
->groupBy(function ($proj) {
return cdate($proj->start_time)->format('Y');
})
->map(function ($items) {
return $items->groupBy(function($item) {
return cdate($item->start_time)->format('m');
})
->map(function($month) {
return $this->sumTime($month);
});
});
Another method using foreach loop
...
$monthlyAmounts = Reservation::all();
$new = [];
foreach($monthlyAmounts as $key => $val)
{
$dis = explode('-', $key);
$new[$dis[0]][$dis[1] = $val;
}
print_r($new);

Transform data with Laravel helpers

I have an array with the following format:
[
{
"stat_month": "01-2019",
"in_sum": 45443,
"out_sum": 42838,
"balance": 2605
},
{...}
]
But I want to transform this array, hopefully in one operation, to this:
[
"labels" => ["01-2019", "02-2019", "03-2019"],
"in_sum" => [45443, 60947, 56734],
"out_sum" => [42838, 42151, 75486],
"balance" => [2605, 18796, -18752]
]
Any ideas how to solve this in one operation with collection helper functions?
Look at mapToGroups in Laravel Collections:
https://laravel.com/docs/5.8/collections#method-maptogroups
or this solution:
$obj1 = new \stdClass;
$obj1->stat_month = "01-2019";
$obj1->in_sum = 45443;
$obj1->out_sum = 42838;
$obj1->balance = 2605;
$obj2 = new \stdClass;
$obj2->stat_month = "02-2019";
$obj2->in_sum = 55443;
$obj2->out_sum = 52838;
$obj2->balance = 3605;
$collection = collect([
$obj1,$obj2
]);
$aResult = [
'labels' => [],
'in_sum' => [],
'out_sum' => [],
'balance' => []
];
$collection->each(function ($item, $key) use (&$aResult) {
$aResult['labels'][] = $item->stat_month;
$aResult['in_sum'][] = $item->in_sum;
$aResult['out_sum'][] = $item->out_sum;
$aResult['balance'][] = $item->balance;
});
Result:
array:4 [▼
"labels" => array:2 [▼
0 => "01-2019"
1 => "02-2019"
]
"in_sum" => array:2 [▼
0 => 45443
1 => 55443
]
"out_sum" => array:2 [▼
0 => 42838
1 => 52838
]
"balance" => array:2 [▼
0 => 2605
1 => 3605
]
]
You can do this is php like this:
<?php
$array = '[
{
"stat_month": "01-2019",
"in_sum": 45443,
"out_sum": 42838,
"balance": 2605
},
{
"stat_month": "01-2019",
"in_sum": 45443,
"out_sum": 42838,
"balance": 2605
},
{
"stat_month": "01-2019",
"in_sum": 45443,
"out_sum": 42838,
"balance": 2605
}
]';
$array = json_decode($array, true);
$arrayResult = [
'stat_month' => array_column($array, 'stat_month'),
'in_sum' => array_column($array, 'in_sum'),
'out_sum' => array_column($array, 'out_sum'),
'balance' => array_column($array, 'balance')
];
echo "<pre>";
print_r($arrayResult);
?>
You can use simplified array_map() and receive desired output:
$i = 0;
$tmp = ['labels'=>[],'in_sum'=>[],'out_sum'=>[],'balance'=>[]];
array_map(function($obj) use (&$tmp, $i){
foreach($tmp as &$val){
$val[] = array_values(((array)$obj))[$i];
$i++;
}
},$ar);
Demo
Or just simple foreach loop:
$tmp = ['labels'=>[],'in_sum'=>[],'out_sum'=>[],'balance'=>[]];
foreach($ar as $obj) {
$i = 0;
foreach($tmp as &$val){
$val[] = array_values(((array)$obj))[$i];
$i++;
}
}
Demo2
You can replace and re-write this code with Laravel map() function. Try to use dynamic loops instead of predefined object's properties.

How do I convert eloquent object to a zero depth array? [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
I have an eloquent object with data from the model which I need to convert to an array and have all indexes from the relations at the same array depth.
I tried array_flatten(), map(), filter() and other PHP methods but couldn't get it working correctly.
How I get my relations:
$data = Person::whereIn('id', $this->id[0])->where('school_id', '=',
$this->school)->with(['personinfo'=>function ($query) {
$query->select(
'person_id',
'general_info',
);
}, 'trades'=>function ($query) {
$query->select('person_id', 'trade_id')->with('trades');
}, 'measurements'=>function ($query) {
$query->select(
'person_id',
'measuring_point_1',
'measuring_point_1_date',
);
}])->get();
return $data->toArray();
What results in the array below, this is as close as I could get using different methods.
This is the outcome of the return function:
array:3 [
1 => array:17 [
"school_id" => 6
"birth_date" => null
"sex_id" => 1
"phone_number" => 62452676867897
"mobile_number" => 62398356786787
"email" => "example#example.com"
"personinfo" => array:5 [
"person_id" => 21
"general_info" => null
"health_info" => null
]
"trades" => array:3 [
"person_id" => 21
"trade_id" => 2
"trades" => array:8 [
"school_id" => 2
"name" => "blablabla"
]
]
"measurements" => array:7 [
"person_id" => 21
"measuring_point_1" => null
"measuring_point_1_date" => null
]
]
];
I need the array to be like this:
array:3 [
1 => array:17 [
"school_id" => 6
"birth_date" => null
"sex_id" => 1
"phone_number" => 624176676867897
"mobile_number" => 649498356786787
"email" => "example#example.com"
"person_id" => 21
"general_info" => null
"health_info" => null
"person_id" => 21
"trade_id" => 2
"school_id" => 2
"name" => "blablabla"
"person_id" => 21
"measuring_point_1" => null
"measuring_point_1_date" => null
]
]
];
Basically, I need to convert the multidimensional array to a zero depth array.
Any help would be appreciated.
You can use custom of array flatten with merging the inner with array-merge as:
function arrayFlatten($array) {
$res = array();
foreach ($array as $k => $v) {
if (is_array($v)) $return = array_merge($res, array_flatten($v)); // add recursively
else $res[$k] = $v; // just add
}
return $res;
}
Now just return arrayFlatten($data->toArray());
Simple live example
How i made it work using dWinder's answer
$data = Person::whereIn('id', $this->id[0])->where('school_id', '=', $this->school)->with(['personinfo'=>function ($query) {
$query->select(
'person_id',
'general_info',
);
}, 'trades'=>function ($query) {
$query->select('person_id', 'trade_id')->with('trades');
}, 'measurements'=>function ($query) {
$query->select(
'person_id',
'measuring_point_1',
'measuring_point_1_date',
);
}])->get();
function array_flatten($array)
{
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$return = array_merge($return, array_flatten($value));
} else {
$return[$key] = $value;
}
}
return $return;
}
foreach ($data as $value) {
$result[] = array_flatten($value->toArray());
}
return $result;

Laravel get group of arrays

I have code below and result is like:
array:2 [▼
0 => array:1 [▼
"CPU" => "AMD a5"
]
1 => array:1 [▼
"CPU" => "AMD a9"
]
]
I want to group them in same array like
CPU => [
"0" => "AMD a5",
"1" => "AMD a9"
]
Code
$category = Category::where('slug', $slug)->where('status', '=', 'active')->first();
$products = $category->products;
foreach ($products as $product) {
foreach($product->attributes as $attribut){
$attributes[] = [$attribut->group->title => $attribut->title];
}
}
What should I change?
If you want a bit of a more scalable solution where you might have multiple attribute types, the following may help:
$attributes = [
[
'CPU' => 'AMD A5'
],
[
'CPU' => 'AMD A9'
],
[
'GFX' => 'AMD'
]
];
return collect($attributes)
->groupBy(function($item) {
return key($item);
})
->map(function($item) {
return array_flatten($item);
})
->toArray();
The output of the above will be:
array:2 [▼
"CPU" => array:2 [▼
0 => "AMD A5"
1 => "AMD A9"
]
"GFX" => array:1 [▼
0 => "AMD"
]
]
Here's an example you can play with.
if your $attribut->group->title is "CPU" and your $attribut->title is "AMD a5"
then you can use this
foreach ($products as $product) {
foreach($product->attributes as $attribut){
$attributes[$attribut->group->title][] = $attribut->title;
}
}
from #mozammil answer if you still wanna do using foreach
$attributes =
[
[
'CPU' => 'AMD A5'
],
[
'CPU' => 'AMD A9'
],
[
'GFX' => 'AMD'
]
];
$data = [];
foreach ($attributes as $titles){
foreach ($titles as $title=> $row){
$data[$title][] = $row;
}
}
You can try collection methods here is an example.
$keyed = $collection->map(function ($item,$key) {
return [[$key] => $item['CPU']];
});
For more information
https://laravel.com/docs/5.7/collections#method-map
Use array_column to get one column of an array.
$category = Category::where('slug', $slug)->where('status', '=', 'active')->first();
$products = $category->products;
$cpu = array_column($products, "CPU");
This will return an array as your expected result.
https://3v4l.org/UYPhK
This requires PHP 7 since in older versions array_column can't handle objects

Laravel map continue

How I can in laravel map function continue loop?
I have code:
return collect($response->rows ?? [])->map(function (array $userRow) {
if ($userRow[0] == 'Returning Visitor') {
return [
$userRow[1] => [
'type' => $userRow[0],
'sessions' => (int) $userRow[2],
]
];
} else {
return false;
}
});
And output:
Collection {#986 ▼
#items: array:4 [▼
0 => false
1 => false
2 => array:1 [▶]
3 => array:1 [▶]
]
}
I don't need params with false, I need continue it or delete. How I can resolve this?
You can add a reject function after the map to remove all values that are false.
return collect($response->rows ?? [])
->map(function (array $userRow) {
if ($userRow[0] == 'Returning Visitor') {
return [
$userRow[1] => [
'type' => $userRow[0],
'sessions' => (int) $userRow[2],
]
];
} else {
return false;
}
})
->reject(function ($value) {
return $value === false;
});
You can use filter() or reject() (inverse of filter) to filter your collection, then map as you need. Something like this:
return collect($response->rows ?? [])->filter(function (array $userRow) {
return $userRow[0] == 'Returning Visitor';
})->map(function (array $userRow) {
return [
$userRow[1] => [
'type' => $userRow[0],
'sessions' => (int) $userRow[2],
]
];
});

Categories