Why are you doing it in blade? blade is just a template engine. You're suppose to do that on the controller.
$q = 'first';
print_r(array_filter($main_array, function($v, $k) use ($q){
return $v['name']==$q;
}));
assuming you're trying this on a select
<select>
@foreach($main_array as $item)
@if($item['name'] == $q)
<option selected value="{{ $item['url'] }}">{{ $item['name'] }}</option>
@else
<option value="{{ $item['url'] }}">{{ $item['name'] }}</option>
@endif
@endforeach
</select>
I know that it should be done in the controller and it is.
@astroanu your answer is 100% right but my question is, is there any other way to get the value instead of using @foreach?
Let's assume that I have 1000 of such arrays in main_array. It means that if the value is on the 909th place, then I will have to check all previous 908 elements to search for the one I need.
Laravel has a many fancy methods and I thought that there may be some other way to do that.
what is your use case ? are you trying to just search the array or are you populating something like a select box ?
$collection = collect($main_array);
$filteredItems = $collection->where('name', 'first');
@astroanu I have arrays made of pair icon name and URL to the image file.
My website has many icons and I want to select only those with specific name and embed them into <img> tag.
Set the key to whatever you are searching for
$main_array['first'] = array("name" => "first", "url" => "http://test.com/");
Then you can just do
echo $main_array[$search]['url'];
Hi, did you even try my solution? if you put the array in to a collection you should be able to search it using where,
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community