PHP- Array combine not working properly - php

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);

Related

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.

add associative arrays with different keys to main associative array

Have problem with associative array.
I have code:
$array[]=[$key=>['value'=>$value,'value1'=>$value1'];
$key value can be repeated, it means that for $key = 4 we can have few options of $value and $value1.
It generates structure as following:
[0]=>[4=>['value'=>$value,'value1'=>$value1'];
[1]=>[4=>['value'=>$value,'value1'=>$value1'];
[2]=>[4=>['value'=>$value,'value1'=>$value1'];
The think is that i want to achieve different structure:
[4]=>[0=>['value'=>$value,'value1'=>$value1'];
[1 =>['value'=>$value,'value1'=>$value1'];
[2 =>['value'=>$value,'value1'=>$value1'];
laravel dd should show it that way:
^ array:1 [▼
4 => array:1 [▼
0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
]
]
Inside array with key 4 i want to put few arrays with keys as following 0,1,2 etc.
I tried like so:
$array[$key]=[['value'=>$value,'value1'=>$value1']];
but its overriding inside array key, and any time its = 0 like here:
[4]=>[0=>['value'=>$value,'value1'=>$value1'];
laravel:
array:1 [▼
4 => array:1 [▼
0 => array:2 [▶]
]
]
What a shame just after post I get an idea.
$array[$key][]=['value'=>$value,'value1'=>$value1'];
That what I was looking for.

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 get keys(index) of collection in laravel

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

PHP Get a specific property from an array of objects with different array count

I am in a confusing situation right now.
So, I have an array of objects
array:3 [▼
0 => array:5 [▶]
1 => array:2 [▶]
2 => array:10 [▶]
]
Each array items contains another array which will have objects
array:3 [▼
0 => array:5 [▼
0 => {#215 ▼
+"DefaultTimeLength": 40
+"ProgramID": 4
+"NumDeducted": 1
+"ID": 245
+"Name": "30-Swedish-Massage"
}
1 => {#216 ▼
+"DefaultTimeLength": 70
+"ProgramID": 4
+"NumDeducted": 1
+"ID": 246
+"Name": "60-Swedish-Massage"
}
2 => {#217 ▶}
3 => {#218 ▶}
4 => {#219 ▶}
]
1 => array:2 [▶]
2 => array:10 [▶]
]
What I want to achieve is, I want to get the 'ID' and 'Name' as an array for every array of objects from this array. Since, every array inside the main array have different counts, I cannot use a FOR loop, to get the required data.
Any ideas?
use nested foreach loop e.g:
foreach($main as $m){
foreach($m as $item){
echo $item->ID ." ".$item->Name;
}
}
use 2 foreach loop inside eachother
foreach ($array as $item) {
foreach ($item as $sub) {
echo $sub['ID'] . " " . $sub['Name'] . "<br>";
}
}
your full code will be something like this

Categories