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.
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
Hi my code is including two array which I want echo them with foreach() loop in table but there is problem that php won’t let me run two array in one loop
like this:
#foreach ($playerinfo1 as $info1 && $playerinfo2 as $info2)
<tr>
<td>{{$info1->id}}</td>
<td>{{$info1->authid}}</td>
<td>{{$info1->nick}}</td>
<td>{{$info1->date}}</td>
<td>{{$info1->uses}}</td>
<td>{{$info2->rounds}}</td>
<td>{{$info2->time}}</td>
<td>{{$info2->connects}}</td>
<td><a href="http://ip-api.com/#{{$info1->ip}}">
{{$info1->ip}}</a></td>
</tr>
#endforeach
I tried these codes but they have problem and they echo the value more than once:
#foreach ($playerinfo1 as $info1)
<tr>
<td>{{$info1->id}}</td>
<td>{{$info1->authid}}</td>
<td>{{$info1->nick}}</td>enter code here
<td>{{$info1->date}}</td>
<td>{{$info1->uses}}</td>
<td><a href="http://ip-api.com/#{{$info1->ip}}">
{{$info1->ip}}</a></td>
#foreach ($playerinfo2 as $info2)
<td>{{$info2->rounds}}</td>
<td>{{$info2->time}}</td>
<td>{{$info2->connects}}</td>
</tr>
#endforeach
#endforeach
#foreach ($playerinfo1 as $info1)
<tr>
<td>{{$info1->id}}</td>
<td>{{$info1->authid}}</td>
<td>{{$info1->nick}}</td>
<td>{{$info1->date}}</td>
<td>{{$info1->uses}}</td>
<td>{{$info1->ip}}</td>
#foreach ($playerinfo2 as $info2)
<td>{{$info2->rounds}}</td>
<td>{{$info2->time}}</td>
<td>{{$info2->connects}}</td>
</tr>
#endforeach
#endforeach
#foreach ($playerinfo1 as $info1)
<tr>
<td>{{$info1->id}}</td>
<td>{{$info1->authid}}</td>
<td>{{$info1->nick}}</td>
<td>{{$info1->date}}</td>
<td>{{$info1->uses}}</td>
<td>{{$info1->ip}}</td>
#endforeach
#foreach ($playerinfo2 as $info2)
<td>{{$info2->rounds}}</td>
<td>{{$info2->time}}</td>
<td>{{$info2->connects}}</td>
</tr>
#endforeach
Also I used array_combine() in order to combining two array but because of differences between key and value ,it doesn't work
$playerinfo1(array):
array:15 [▼
0 => {#213 ▼
+"id": 1
+"authid": "STEAM_0:0:546411185"
+"nick": "BesTKiLLeR"
+"date": "2019-05-24 21:22:25"
+"uses": 62
+"ip": "188.211.128.180"
}
1 => {#215 ▼
+"id": 2
+"authid": "STEAM_0:0:21578434"
+"nick": "ArTTam"
+"date": "2019-05-23 22:29:43"
+"uses": 21
+"ip": "86.55.174.70"
}
2 => {#216 ▶}
3 => {#217 ▶}
9 => {#223 ▶}
10 => {#224 ▶}
11 => {#225 ▶}
12 => {#226 ▶}
26 => {#240 ▶}
32 => {#246 ▶}
34 => {#248 ▶}
35 => {#249 ▶}
38 => {#252 ▶}
39 => {#253 ▶}
45 => {#259 ▶}
]
$playerinfo2(array):
array:16 [▼
0 => {#264 ▼
+"id": 1
+"connects": 65
+"rounds": 305
+"time": 38579
}
1 => {#265 ▼
+"id": 2
+"connects": 37
+"rounds": 124
+"time": 17257
}
2 => {#266 ▶}
3 => {#267 ▶}
4 => {#268 ▶}
5 => {#269 ▶}
6 => {#270 ▶}
7 => {#271 ▶}
8 => {#272 ▶}
9 => {#273 ▶}
10 => {#274 ▶}
11 => {#275 ▶}
12 => {#276 ▶}
13 => {#277 ▶}
14 => {#278 ▶}
15 => {#279 ▶}
]
I’m afraid if I could not make my point but I hope you will understand what am I mean by looking this codeenter code here
You are using Laravel and your data is coming from two tables. Why not use an eloquent relationship? It would be so much easier.
#foreach ($players as $player)
<tr>
<td>{{$player->id}}</td>
<td>{{$player->authid}}</td>
<td>{{$player->nick}}</td>enter code here
<td>{{$player->date}}</td>
<td>{{$player->uses}}</td>
<td>{{$player->ip}}</td>
<!-- Use a relationship (e.g., stats) to get additional data -->
<td>{{$player->stats->rounds}}</td>
<td>{{$player->stats->time}}</td>
<td>{{$player->stats->connects}}</td>
</tr>
#endforeach
You can use array_merge($arr1,$arr2) in your controller
to combine two arrays
I'm connecting to a 3rd party API can dd the complete result. As a test I'm getting all countries with a name and a type. Now I would like to show just 1 countryname as a test. I'm not sure how to do this.
I tried $countries[0]->name and $countries[0]['name'] but both don't seem to work
Current result:
array:69 [▼
0 => NamedCriterion {#218 ▼
-name: "Andorra"
-type: "Country"
-value: "13"
}
1 => NamedCriterion {#235 ▶}
2 => NamedCriterion {#233 ▶}
3 => NamedCriterion {#220 ▶}
4 => NamedCriterion {#230 ▶}
5 => NamedCriterion {#223 ▶}
6 => NamedCriterion {#221 ▶}
7 => NamedCriterion {#219 ▶}
From this call:
$response = $search->getCriteria([], ['Country'], []);
if (!$response->isError()) {
$criterionSets = $response->getCriterionSets();
$countryCriterionSet = $criterionSets[0];
$countries = $countryCriterionSet->getCriteria();
$resultCount = $response->getResultCount();
}
dd($countries);
}
So I would for example just get Andorra as a result of my call and dd that.
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.