Support the ongoing development of Laravel.io →
posted 8 years ago
Blade
Last updated 1 year ago.
0

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>
Last updated 8 years ago.
0

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.

0

what is your use case ? are you trying to just search the array or are you populating something like a select box ?

0
$collection = collect($main_array);
$filteredItems = $collection->where('name', 'first');
0

@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.

0

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'];
0

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,

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Lucassith lucassith Joined 8 Dec 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.