Get the first index of asssoc array in Laravel blade - php

I have a following assoc array:
45111 => array:16 [▼
"სქესი" => array:1 [▶]
"ასაკი" => array:1 [▶]
"შემოსავალი" => array:1 [▶]
"შემოსავლის ტიპი" => array:1 [▶]
"Existing PMT" => array:1 [▶]
"min(payment cnt)" => array:1 [▶]
"guarantor" => array:1 [▶]
"reqeusts last 3m / req last 12m" => array:1 [▶]
"micro loans cnt last 12m (closed)" => array:1 [▶]
"micro loans cnt last 3m (closed)" => array:1 [▶]
"loans deliquency days" => array:1 [▶]
"Past PMT" => array:1 [▶]
"RiskGrade" => array:1 [▶]
"ProbabilityOfDefault" => array:1 [▶]
"Total" => array:1 [▶]
"penalties paid" => array:1 [▼
0 => 90.0
]
]
I'd like to iterate over the second array using first index. However, it returns "Undefined offset" exception. I guess $v[0] is not supported in blade. What is the correct way to do this?
#foreach ($creditInfo as $k => $v)
#foreach ($v[0] as $y => $x)
<tr>
<td>{{$y}}</td>
</tr>
#endforeach
#endforeach

It's just executing PHP so to say it's not supported in Blade makes no sense. The error happens because $v[0] does not exist as 0 is an undefined offset. $v['Total'] would be fine though and would return an array.
Since the first key is სქესი, 0 isn't going to work but if you are only concerned about the first item, you can use current to grab the first.
#foreach ($creditInfo as $k => $v)
#foreach (current($v) as $y => $x)
<tr>
<td>{{$y}}</td>
</tr>
#endforeach
#endforeach
That will loop through the array in $v['სქესი']

Related

How to update a nested value within a nested Laravel collection

I have a deployments Laravel Collection like this:
Illuminate\Support\Collection {#415 ▼
#items: array:5 [▼
0 => array:7 [▼
"id" => 31
"status" => "active"
"name" => "Deployment 1"
"spots" => array:4 [▼
0 => array:2 [▼
"id" => 33
"status" => "active" <-- Want to change this
]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:2 [▶]
]
"data" => array:3 [▶]
]
1 => array:7 [▶]
2 => array:7 [▶]
3 => array:7 [▶]
4 => array:7 [▶]
]
}
I want to update the nested status value to inactive. I have used the Laravel map function, but it only seems to work on collections that have one nesting level. So this...
$this->deployments->map(function ($deployment) {
$deployment['spots'][0]['status'] = 'inactive';
});
dd($this->deployments);
...leaves $this->deployments untouched.
Also tried using nested map functions obtaining a Call to a member function map() on array exception on second level as second and next nesting levels are considered arrays...
Any thoughts?
Thanks in advance.
With the map method, you are almost there. You will have to return the change made in $deployment and do an ->all() at the end to update the collection with the modified values.
For updating a single spot:
$deployments = $deployments->map(function($deployment){
$deployment['spots'][0]['status'] = 'inactive';
return $deployment;
})->all();
For updating all spots:
$deployments = $deployments->map(function($deployment){
foreach($deployment['spots'] as &$spot){
$spot['status'] = 'inactive';
}
return $deployment;
})->all();
For anyone researching this, a more elegant solution might be:
For updating a single spot:
$data = $deployments->all();
$deployments = data_set($data, 'spots.0.status', 'inactive');
For updating all spots:
$data = $deployments->all();
$deployments = data_set($data, 'spots.*.status', 'inactive');

Laravel Object of class stdClass could not be converted to string. Still object when using toArray();

I have a table that I want to export via excel. I use the method toArray(); and still I get the result as object. Here is my sample code
$items = \DB::table('users')
->join('finances', 'users.id','=','finances.user_id')
->join('schoolyears', 'users.school_id','=','schoolyears.school_id')
->select('users.name','users.phone','users.section_id', 'users.student_school_id','finances.amount','finances.description','schoolyears.name as syear','finances.date')
->where('finances.date', '=' ,(\DB::raw("(select max(`date`) from finances f where finances.user_id=f.user_id)")))
->where('users.role','=','4' )
->where('users.school_id','=', $sid)
->get()->toArray();
// dd($items);
} else {
return redirect('home')->with('error', 'Invalid access');
}
\Excel::create($this->page_title . 's', function ($excel) use ($items) {
$excel->sheet($this->page_title . 's', function ($sheet) use ($items) {
$sheet->fromArray($items);
});
The result I get when i dd($items)
array:2 [▼
0 => {#558 ▼
+"name": "Annamarie Morar"
+"phone": "(0997) 212-7919"
+"section_id": null
+"student_school_id": "50"
+"amount": "500"
+"description": "New Pays"
+"syear": "SY-2019-2020"
+"date": "2019-11-14"
}
1 => {#561 ▶}
]
What i want is like this so that i can export it as an excel file
array:9 [▼
0 => array:10 [▼
"FirstName" => "Madelynn"
"LastName" => "Stokes"
"Gender" => "female"
"Birthday" => "2013-10-09"
"Address" => "78A/40 Goodwin Meadow, Poblacion, Iloilo City 1333 Nueva Ecija"
"PhoneNo" => "+63 (971) 659-8143"
"Parent" => "Deondre Stokes"
"SchoolID" => "521"
"RFID" => "173"
"Section" => null
]
1 => array:10 [▶]
2 => array:10 [▶]
3 => array:10 [▶]
4 => array:10 [▶]
5 => array:10 [▶]
6 => array:10 [▶]
7 => array:10 [▶]
8 => array:10 [▶]
]
You could convert each object to an array by transforming the Collection then turning the Collection into an array if you had to:
$items = DB::table(...)->.....->get()->transform(function ($item) {
return (array) $item;
})->toArray();
Laravel 6.x Docs - Collections - Methods - transform

How to retrieve nested array data in PHP/Laravel?

{`
+"insights": array:1 [▼
0 => {#209 ▼
+"group": "Provision"
+"dataset": array:1 [▼
0 => {#207 ▼
+"group": "Provision"
+"set": array:3 [▼
0 => {#194 ▼
+"name": "Neutral"
+"value": 917
}
1 => {#203 ▶}
2 => {#197 ▶}
]
}
]
}
]
+"errorCode": 0
}`
How to get the name property inside the set array? I've tried multiple way but it kept error return trying to get property non-object.
suppose you provide $response to your blade view
$response = {
+"insights": array:1 [▼
0 => {#209 ▼
+"group": "Provision"
+"dataset": array:1 [▼
0 => {#207 ▼
+"group": "Provision"
+"set": array:3 [▼
0 => {#194 ▼
+"name": "Neutral"
+"value": 917
}
1 => {#203 ▶}
2 => {#197 ▶}
]
}
]
}
]
+"errorCode": 0
}
You have to loop through to response , in your blade view :
#foreach($response->insights as $insight)
#foreach($insight['dataset'] as $dataset)
#foreach($dataset['set'] as $set)
<tr><td>$set['name']</td></tr>
#endforeach
#endforeach
#endforeach
data_get($data, 'insights.0.dataset.0.set.0.name');
end if you have json - convert it into array -> json_decode(string);
You have to simply loop through it and do whatever you want to with that name,
foreach($response->insights as $temp){
foreach($temp->dataset as $var){
foreach($var as $obj){
$name = $obj->name;
}
}
}

Laravel - How to collect multiple array to single one

I've created a collection like this :
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:2 [▶]
]
1 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#661 ▶}
"foods" => array:2 [▶]
]
2 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#665 ▶}
"foods" => array:2 [▶]
]
]
}
I demand to achieve collection like this (with Laravel collection):
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:6 [▶]
]
]
}
Because orderId and orderCreated are all the same in arrays. I need to make single array that collect orderId and orderCreated with all foods.
Any suggestion?
You can take the data from the first one and just combine all of the food items. For instance like this:
$new = collect([
'orderId' => $old->first()->orderId,
'orderCreated' => $old->first()->orderCreated,
'foods' => $old->pluck('foods')->flatten(1),
]);
The exact implementation will depend on how you built your initial collection.

array_search in php doesn't find my string

So I'm trying to make a function that searches a string in a whole array. But it give me anything... So my array looks like this :
array:1 [▼
"list" => array:2 [▼
"pagination" => array:5 [▶]
"entries" => array:11 [▼
0 => array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
]
]
]
So for exemple at first I did $key = array_search("doctor", $users); but this gives me nothing. So I thought it was because I have a multidimensional array. So I reduced it to just one array (and I would search in the rest of the original array with a for loop), so now I'm working with this array that I got with $users['list']['entries'][0]
array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
But $key = array_search("doctor", $users['list']['entries'][0]); still doesn't give me anything (but false).
Does anyone know where is my mistake ? Because I couldn't find a solution of my problem yet and it's been a pretty long time I'm on it... I'm still a beginner in php so maybe I've missed something obvious and i'm sorry if I did.
Thank you in advance !
you are searching for doctor,but value stored in your array is as Doctor. So either search for Doctor as
$key=array_search('Doctor', $users['list']['entries'][0]));
or for case in-sensetive search use
$key=array_search(strtolower('Doctor'), array_map('strtolower', $users['list']['entries'][0]));
Note: as your array is multidimensional, so use loop to search your value

Categories