Laravel illluminati collection remove duplicate item - php

I have a collection called $applied_exams which contain following data
Collection {#2033 ▼#items: array:4 [▼
0 => {#2035 ▼
+"post_id": 1
+"designation_id": 9
+"token_number": 438
+"name": "TECHNICIAN TELECOM"
+"adb": "2074-06-23"
+"ContainMultiplePost": false
+"Group_id": 8
}
1 => {#2034 ▼
+"post_id": 11
+"designation_id": 1
+"token_number": 740
+"name": "DEPUTY MANAGER TELECOM"
+"adb": "2074-06-25"
+"ContainMultiplePost": false
+"Group_id": 1
}
2 => {#2040 ▼
+"post_id": 3
+"designation_id": 12
+"token_number": 2810
+"name": "ASST. BUSINESS OFFICER"
+"adb": "2074-10-24"
+"ContainMultiplePost": true
+"Group_id": 7
}
3 => {#2039 ▼
+"post_id": 2
+"designation_id": 8
+"token_number": 2811
+"name": "ASST. ADMINISTRATIVE OFFICER"
+"adb": "2074-10-24"
+"ContainMultiplePost": true
+"Group_id": 7
}]}
I want to unset the data with duplicate group_id. I only want one data of one group_id. In this case I just want the $applied_exam[3] to be removed because group_id is same as $applied_exams[2]. Please help. Thanks

Use unique function
$unique_applied_exams = $applied_exams->unique('group_id');

Related

Merge a Laravel array of Collections, and add new properties/attributes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a collection array of 'Products', as seen from the dump below.
Illuminate\Support\Collection {#918 ▼
#items: array:12 [▼
0 => App\Product {#934 ▶}
1 => App\Product {#943 ▶}
2 => App\Product {#959 ▶}
3 => App\Product {#968 ▶}
4 => App\Product {#984 ▶}
5 => App\Product {#993 ▶}
6 => App\Product {#1009 ▶}
7 => App\Product {#1018 ▶}
8 => App\Product {#1034 ▶}
9 => App\Product {#1043 ▶}
10 => App\Product {#1059 ▶}
11 => App\Product {#1068 ▶}
]
}
This array has ONLY two unique products;
App\Product {#934 ▼
...
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"slug" => "26027686555545"
"details" => App\DispatchStock {#900 ▼
...
#attributes: array:9 [▼
...
"quantity" => 3
"created_at" => "2020-05-04 15:56:07"
]
...
}
]
...
}
and
App\Product {#943 ▼
...
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 2
"slug" => "26027687444410"
"details" => App\DispatchStock {#899 ▼
...
#attributes: array:9 [▼
...
"quantity" => 5
"created_at" => "2020-05-04 15:56:07"
]
...
}
]
...
}
duplicated 6 times each (with varying 'quantity's).
I want to loop through the collection array of 'Products', merge all the duplicates, and return an array of just the unique products.And have something like:
Illuminate\Support\Collection {#918 ▼
#items: array:2 [▼
0 => App\Product {#934 ▶}
1 => App\Product {#943 ▶}
]
}
While appending a new attribute 'combined_available_quantity' (the sum of all the 'quantity's of the Products) to the 'details' property of each of the unique Products.
For example, I want one of the final 'Product's to look like:
App\Product {#943 ▼
...
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 2
"slug" => "26027687444410"
"details" => App\DispatchStock {#899 ▼
...
#attributes: array:9 [▼
...
"quantity" => 5
"created_at" => "2020-05-04 15:56:07"
"combined_available_quantity" => 27
]
...
}
]
...
}
I already started with the code below.
...
$combinedProducts = collect();
foreach ($products as $product) {
$combinedProducts = $combinedProducts->each(function($combinedProduct) use ($combinedProducts, $product) {
if ($combinedProduct->slug == $product->slug) {
$qty = $product->details->quantity + $combinedProduct->details->quantity;
$combinedProduct->details->setAttribute('combined_available_quantity', $qty);
} else {
$product->details->setAttribute('combined_available_quantity', $product->details->quantity);
$combinedProducts->push($product);
}
});
$combinedProducts = $combinedProducts->values();
}
But it's buggy, and doesn't give me required results.
I need a better approach. Thanks.
$collect->groupBy('slug')->map(function($item){
return $item->sum('quantity');
})

remove duplicates from a group of arrays

I have the following group of arrays in one array, where the content of them is a group of posts :
array:5 [▼
"in" => array:2 [▼
0 => {#371 ▼
+"id": 61
+"created_at": "2019-11-29 08:12:11"
+"updated_at": "2019-11-29 08:12:11"
+"title": "Sports test "
+"body": "Test answer"
+"ttype": 0
+"cat": 0
+"a_id": 61
+"tag": ""
}
1 => {#372 ▶}
]
"out" => []
"other" => []
"fol" => array:17 [▶]
"tag" => []
]
The arrays :
$Final_updts['in'] = $all_update_in;
$Final_updts['out'] = $all_update_out;
$Final_updts['other'] = $all_update_oth;
$Final_updts['fol'] = $follow_psts;
$Final_updts['tag'] = $post_tags;
There might be some posts repeated in more than one array. I tried to use array_unique($Final_updts);, but received Error exception: Array to string conversion
Is there any easy way to do this, or I have to modify the 5 queries to prevent any duplicate results ?
I assume that you want to get unique items by id. Lets use collection to solve this problem.
$results = collect($array)->collapse()->unique('id');
Pls let me know if I got it right.

How to get columns name from sql server view by Laravel

I am trying to get columns name from sql view, my view appear like this:
name type building_id
test type_test 5
test2 type2_test 6
I want to get names(name & type & building_id)
I tried Laravel function getColumnListing() but i get null array
Try this
\Illuminate\Support\Facades\DB::select('show columns from table_name');
// table_name must be db table name
Result
array:5 [▼
0 => {#309 ▼
+"Field": "id"
+"Type": "int(10) unsigned"
+"Null": "NO"
+"Key": "PRI"
+"Default": null
+"Extra": "auto_increment"
}
1 => {#311 ▼
+"Field": "name"
+"Type": "varchar(100)"
+"Null": "NO"
+"Key": ""
+"Default": null
+"Extra": ""
}
2 => {#312 ▶}
3 => {#313 ▶}
4 => {#314 ▶}
]

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!

Loop through JSON array to get score - Laravel 5.2

I'am having problem looping through an array to get every teams score for the last 10 games played by a player. This is how I'm looping through this array:
$WarzoneLast10MatchesTeamScore = [];
foreach($warzoneLast10matches->Results->Teams as $idx => $stats){
$WarzoneLast10MatchesTeamScore[$idx]['Score'] = $stats->Score;
$WarzoneLast10MatchesTeamScore[$idx]['Id'] = $stats->Id;
}
The problem with this is it will give me an error because I'am trying to get the last 10 games played, so result has to be then Result[0], Result[1], and so on.
Here is what I mean:
+"Results": array:10 [▼
0 => {#17371 ▼
+"Links": {#13129 ▶}
+"Id": {#13130 ▶}
+"HopperId": "0e39ead4-383b-4452-bbd4-babb7becd82e"
+"MapId": "c89dae21-f206-11e4-a1c2-24be05e24f7e"
+"MapVariant": {#13121 ▶}
+"GameBaseVariantId": "42f97cca-2cb4-497a-a0fd-ceef1ba46bcc"
+"GameVariant": {#17372 ▶}
+"MatchDuration": "PT6M50.2813116S"
+"MatchCompletedDate": {#17367 ▶}
+"Teams": array:2 [▼
0 => {#17374 ▼
+"Id": 0
+"Score": 1
+"Rank": 1
}
1 => {#17375 ▼
+"Id": 1
+"Score": 0
+"Rank": 2
}
]
+"Players": array:1 [▶]
+"IsTeamGame": true
+"SeasonId": null
+"MatchCompletedDateFidelity": 1
}
1 => {#17378 ▶}
2 => {#17390 ▶}
3 => {#17402 ▶}
4 => {#17414 ▶}
5 => {#17426 ▶}
6 => {#17438 ▶}
7 => {#17450 ▶}
8 => {#17462 ▶}
9 => {#17474 ▶}
]
I obviously don't want to make 10 loops for each game then hard code the score for each game in my view.
How can I loop through the Results object, and then get the Teams->score and Teams->Id objects?
FYI
I know I can use collections like this:
public function getWarzoneLast10Matches($warzoneLast10matches) {
// Collect al the results for this array
$results = collect($warzoneLast10matches->Results);
$array = $results->map(function($item, $key) {
return [
'Gamertag' => $item->Players[0]->Player->Gamertag,
'MapId' => $item->MapId,
'GameBaseVariantId' => $item->GameBaseVariantId,
'Score' => $item->Teams[0]->Score,
'Score2' => $item->Teams[1]->Score,
'Id' => $item->Teams[0]->Id,
'Id2' => $item->Teams[1]->Id,
];
});
return $array;
}
But this will not work because in some games there is only 1 team, and if that happens, then it will throw me an error saying undefined offset 1, because there is no team 2. The other method I'am using on top wont give me errors.
It sound like you might need to make your function recursive.
Learn more about Recursion

Categories