How To get keys(index) of collection in laravel - php

I am using laravel and I have one collection and there are some collection in it so I want to get index of each collection
my collection is like this
Collection {#4415 ▼
#items: array:14 [▼
"01" => Collection {#4311 ▶}
"02" => Collection {#4318 ▶}
"07" => Collection {#4325 ▶}
10 => Collection {#4338 ▶}
12 => Collection {#4345 ▶}
13 => Collection {#4352 ▶}
14 => Collection {#4359 ▶}
18 => Collection {#4366 ▶}
20 => Collection {#4373 ▶}
21 => Collection {#4380 ▶}
25 => Collection {#4387 ▶}
26 => Collection {#4400 ▶}
27 => Collection {#4407 ▶}
31 => Collection {#4414 ▶}
]
}
I want to get for example 01 02 03 04 05 they are days of the month and I want to use them for chart js

You can use filter collection helper.
$result = $yourCollection->filter(function ($value, $key) {
return in_array($key, ['01', '02', '03', '04', '05']);
});
Or externally if you want to pass that array,
$monthArr = ['01', '02', '03', '04', '05'];
$result = $yourCollection->filter(function ($value, $key) use($monthArr)
{
return in_array($key, $monthArr);
});
EDIT
If you want to get only keys of collection then,
$keys = $yourCollection->keys();
dd($keys);
Here is link of official documentation.

if you want to get only the keys of the collections you can use keys method

Related

laravel random matching values from 2 arrays

I have 2 arrays:
one for users and other one to locations
how can I random between users array and locations array
For example:
locations array :
Illuminate\Support\Collection {#4291 ▼
#items: array:20 [▼
0 => {#4312 ▶}
1 => {#4318 ▶}
2 => {#4313 ▶}
3 => {#4315 ▶}
4 => {#4316 ▶}
5 => {#4319 ▶}
6 => {#4320 ▶}
7 => {#4321 ▶}
8 => {#4322 ▶}
9 => {#4323 ▶}
10 => {#4324 ▶}
11 => {#4325 ▶}
12 => {#4326 ▶}
13 => {#4327 ▶}
14 => {#4328 ▶}
15 => {#4329 ▶}
16 => {#4330 ▶}
17 => {#4331 ▶}
18 => {#4332 ▶}
19 => {#4333 ▼
+"business_location_id": 46
+"business_location_name": "Khan El-Moaz"
}
]
}
and users array:
array:2 [▼
167 => "167"
199 => "199"
]
Now I need to get random values from the 2 arrays together like this
[▼
{
"user_id": "167",
"location_id": "20"
},
{
"user_id": "199",
"location_id": "2"
},
{
"user_id": "150",
"location_id": "5"
},
]
I did it by this steps:
First: convert the collection to array
$business_locations = $results->toArray();
then make foreach on one of the aaray and get random key from the second array and create new array with data from first and second array then remove the use data from the array
$final_result = [];
foreach($users as $user)
{
$randomItems = array_rand($business_locations,1);
$collection = collect($final_result);
if(!$collection->contains('location_id', $business_locations[$randomItems]->business_location_id))
{
array_push($final_result, (object)[
'user_id' => $user[user_id],
'user_name' => $user[user_name],
'location_id' => $business_locations[$randomItems]->business_location_id,
'location_name' => $business_locations[$randomItems]->business_location_name,
]);
array_splice($business_locations, $randomItems, 1);
}
}

How do I add new item with key to collection in Laravel?

I have a Laravel collection $Data. When I code outputs
dd($Data); // it outputs the following
Illuminate\Support\Collection {#1344 ▼
#items: array:3 [▼
0 => {#1342 ▶}
1 => {#1334 ▶}
2 => {#1346 ▶}
]
}
$Data->push(['Total'=>600]);
dd($Data);
When I push a Total, it does inserts but output is not as expected.
Illuminate\Support\Collection {#1344 ▼
#items: array:4 [▼
0 => {#1342 ▶}
1 => {#1334 ▶}
2 => {#1346 ▶}
3 => array:1 [▼
"Total" => 600
]
]
}
How do I get 3 => {#1353▶) ?
You're pushing an Array to $Data, while the other items in that Collection are Objects (stdClass or similar). To make this consistent, use casting:
$Data->push((object)['Total' => 600]);
Now you should see 3 => {#...} instead of 3 => array:1.

how to change the format of a array to collection as each item in laravel

i have an array of collections like below :
array:9 [▼
0 => Collection {#990 ▶}
1 => Collection {#1109 ▶}
2 => Collection {#1221 ▶}
3 => Collection {#1331 ▶}
4 => Collection {#1442 ▶}
5 => Collection {#1554 ▶}
6 => Collection {#1664 ▶}
7 => Collection {#1775 ▶}
8 => Collection {#1887 ▶}
]
i want to make this a single collection and make each collection as 1 item of that collection now what i tried is collect($f) but i get the result as below :
Collection {#1443 ▼
#items: array:9 [▼
0 => Collection {#990 ▶}
1 => Collection {#1109 ▶}
2 => Collection {#1221 ▶}
3 => Collection {#1331 ▶}
4 => Collection {#1442 ▶}
5 => Collection {#1554 ▶}
6 => Collection {#1664 ▶}
7 => Collection {#1775 ▶}
8 => Collection {#1887 ▶}
]
}
now i want to know how can i make this 1 collection and make all those 8 collection as items of that collection like below :
Collection {#990 ▼
#items: array:1 [▼
0 => RoomPricingHistory {#971 ▶}
1 => RoomPricingHistory {#971 ▶}
2 => RoomPricingHistory {#971 ▶}
3 => RoomPricingHistory {#971 ▶}
4 => RoomPricingHistory {#971 ▶}
]
}
thanks
Once you have a collection of collections, you can use flatten to get all elements of the underlying collections in the parent collection.
collect($f)->flatten(1);
I'm not sure if this is what you're after.
First, using artisan I'll make a collection of collections. Each collection has a single element array [1]
$ php artisan tinker
>>> $a = collect(1)
=> Illuminate\Support\Collection {#3205
all: [
1,
],
}
>>> collect(array($a,$a,$a,$a,$a,$a,$a))
=> Illuminate\Support\Collection {#3218
all: [
Illuminate\Support\Collection {#3205
all: [
1,
],
},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
],
}
Now, to get only an array of those elements, I use the flatten() method.
>>> collect(array($a,$a,$a,$a,$a,$a,$a))->flatten()
=> Illuminate\Support\Collection {#3220
all: [
1,
1,
1,
1,
1,
1,
1,
],
}
The flatten method accepts an optional depth paramether. Read more about it in the documentation

How to combine the output from an object and an array?

I have a db query that returns a value to the $result variable that looks like this:
Collection {#710 ▼
#items: array:16 [▼
0 => {#717 ▼
+"utest_step_id": 18
+"value": "1"
+"count": 26
}
1 => {#716 ▼
+"utest_step_id": 18
+"value": "2"
+"count": 23
}
2 => {#709 ▶}
3 => {#711 ▶}
4 => {#713 ▶}
5 => {#714 ▶}
6 => {#715 ▶}
7 => {#718 ▶}
8 => {#719 ▶}
9 => {#720 ▶}
10 => {#721 ▶}
11 => {#722 ▶}
12 => {#723 ▶}
13 => {#724 ▶}
14 => {#725 ▶}
15 => {#726 ▶}
]
}
I have an array called $steps that I need to combine with the data that looks like this:
[▼
0 => {#676 ▼
+"id": 18
+"step": 1
+"type": "select many image"
+"featured": false
+"data": {#664 ▼
+"description": "Ut quaerat ut sed molestiae."
+"randomize": 0
+"options": array:4 [▼
0 => {#671 ▼
+"position": 1
+"image_url": "http://lorempixel.com/455/255/?67678"
+"caption": "Prof."
+"image_source": "http://lorempixel.com/455/255/?67678"
}
1 => {#668 ▼
+"position": 2
+"image_url": "http://lorempixel.com/455/255/?23876"
+"caption": "Ms."
+"image_source": "http://lorempixel.com/455/255/?23876"
}
2 => {#670 ▼
+"position": 3
+"image_url": "http://lorempixel.com/455/255/?45833"
+"caption": "Prof."
+"image_source": "http://lorempixel.com/455/255/?45833"
}
3 => {#677 ▼
+"position": 4
+"image_url": "http://lorempixel.com/455/255/?83800"
+"caption": "Dr."
+"image_source": "http://lorempixel.com/455/255/?83800"
}
]
}
}
1 => {#690 ▶}
2 => {#697 ▶}
3 => {#704 ▶}
4 => {#706 ▶}
]
I am trying to insert the value from count in $results into the $steps->data->options array.
Here is my code:
foreach ($results as $result) {
$comparison_field = 'position';
for ($i=0; $i < sizeof($steps); $i++) {
if ($result->utest_step_id == $steps[$i]->id) {
foreach ($steps[$i]->data->options as $option) {
$option->count = 0;
$option->count = $result->count;
}
}
}
}
The problem is I am ending up with the count from the last $result->utest_step_id in each count item in the $option.
Where am I going wrong?
You have a for loop iterating over all of the elements in $steps for each entry in $results. This results in everything in $steps being overwritten for each element in $results. If these arrays are supposed to line up, grab an $id from the $results loop:
foreach ($results AS $id=>$result)
and use that instead of $i in a for loop.

PHP- Array combine not working properly

I am trying to do array combine, but it is not working properly. I have one array called $models which consits of objects and it looks like this:
array:5 [▼
0 => Comment {#377 ▶}
1 => Thumb {#378 ▶}
2 => View {#379 ▶}
3 => Vote {#380 ▶}
]
Then since I am passing it to another function, I am adding one more object as an element like this:
array_push($models, new User);
And then I get an array that looks like this:
array:5 [▼
0 => Comment {#377 ▶}
1 => Thumb {#378 ▶}
2 => View {#379 ▶}
3 => Vote {#380 ▶}
4 => User {#399 ▶}
]
I am then doing foreach loop to get the total count in the DB for each model like this:
foreach ($models as $model){
$modelCounts[] = $model->count();
}
My $modelCounts than looks like this:
array:5 [▼
0 => 19
1 => 22
2 => 15
3 => 17
4 => 3
]
And then I am trying to do array_combine so that my objects are keys and the counts are values like this:
$result = array_combine($models, $modelCounts);
But something is not working right because when I do dd($result); I get:
array:1 [▼
"[]" => 3
]
But when I do it the other way around like this:
$result = array_combine($modelCounts, $models);
It works fine and I get:
array:5 [▼
19 => Comment {#377 ▶}
22 => Thumb {#378 ▶}
15 => View {#379 ▶}
17 => Vote {#380 ▶}
3 => User {#399 ▶}
]
But I need it the other way around and not like this.
Objects can't be used as key for associative arrays, only scalar values are allowed.
http://php.net/manual/en/language.types.array.php
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
The reason why the first array_combine() fails is that an objcect cannot be usead as an array key.
You may want to create an array containing classes names first by using get_class() to get class's name and then combine it with $modelCounts
It should look something like this
foreach ($models as $model){
$modelNames[] = get_class($model);
}
$result = array_combine($modelNames, $modelCounts);

Categories