Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

liandhas's avatar

How to create a filter from an Array?

Array
(
    [0] = Array
        (
            [main_catg] = Home Appliances
            [sub_catg] = Coffee Makers
            [child_catg] = Arabic
        )

    [2] = Array
        (
            [main_catg] = Food
            [sub_catg] = Coffee & Tea
            [child_catg] = Arabic Coffee
        )

    [3] = Array
        (
            [main_catg] = Home Appliances
            [sub_catg] = Coffee Makers
            [child_catg] = Turkish
        )

    [4] = Array
        (
            [main_catg] = Food
            [sub_catg] = Coffee & Tea
            [child_catg] = Turkish Coffee
        )

)

How to get filter as below from the above array?

- Home Appliances
     - Coffee Makers
          - Arabic 
          - Turkish

- Food
     - Coffee & Tea
          - Arabic Coffee
          - Turkish Coffee

I am not an expert in coding, I tried many ways to get this result. Any Suggestions? Thanks in Advance.

0 likes
6 replies
Sinnbeck's avatar

Do you mean change the structure to how yours look (array form). Or output it in html? Or? A filter makes little sense with what you have shown

liandhas's avatar

@tdamir Actually I stored this array as a JSON_encode in a cell for each product.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Ok then you need the right structure that you can loop over

$final = collect($array)->groupBy(['main_catg', 'sub_catg']);
dd($final);

And blade (made from memory so might need tweaking)

@foreach ($final as $main_catg => $sub) 
    {{$main_catg}} 
    @foreach ($sub as $sub_catg => $items)
          {{sub_catg}} 
          @foreach ($items as $item)
                {{item['child_catg']}} 
          @endforeach 
     @endforeach 
@endforeach
1 like

Please or to participate in this conversation.