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

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.

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 tou get values from an array in Laravel

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.

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

Create dynamic table with multi dimensional array data?

Hello experts I am new in laravel and php. I have an multi dimensional array and with this array data I want to create a dynamic table and its maximum count will be 10 as a newbie I can't reach to the perfect result. My array as bellow:
array:4 [▼ 0 => array:2 [▼
0 => {#404 ▼
+"id": 290
+"amount": "8500.00"
}
1 => {#403 ▼
+"id": 399
+"amount": "8500.00"
} ] 1 => array:4 [▼
0 => {#402 ▼
+"id": 107
+"amount": "6590.00"
}
1 => {#401 ▼
+"id": 355
+"amount": "6590.00"
}
2 => {#400 ▼
+"id": 698
+"amount": "6590.00"
}
3 => {#399 ▼
+"id": 734
+"amount": "6590.00"
} ] 2 => array:1 [▼
0 => {#108 ▼
+"id": 21
+"amount": "3240.00"
} ] 3 => array:2 [▼
0 => {#397 ▼
+"id": 27
+"amount": "3030.00"
}
1 => {#396 ▼
+"id": 50
+"amount": "3030.00"
} ] ]
And With this array I want to create a table like as bellow:
Serial ID
Amount 1 290
8500 1 399
8500
2 107
6590 2 355
6590 2 698
6590 2 734
6590
3 108
3240
4 27 3030
4 50 3030
And I am trying with this in laravel :
$flag = 0;
$tableDesign = '';
for($i=0;$i<count($data);$i++) {
$tableDesign .="<tr><td>".$flag++."</td><td>".$data[$i][0]."</td> <td>".$data[$i][1]."</td></tr>";
if($flag == 10)
{ return;}
}
Thanks in advance.
You can rearrange your array before displaying in the table like this:
$newData = array();
$flag = 1;
foreach($data as $key => $elements) {
if($flag > 10) {
break;
}
foreach($elements as $element) {
$newDataElement = array(
'serial' => $key + 1,
'id' => $element['id'],
'amount' => $element['amount']
);
array_push($newData, $newDataElement);
}
$flag++;
}
Then you can display it simply:
<?php foreach($newData as $element): ?>
<tr>
<td><?php print $element['serial']?></td>
<td><?php print $element['id']?></td>
<td><?php print $element['amount']?></td>
</tr>
<?php endforeach; ?>

Categories