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

Rediska's avatar

How to send a query to a database given a two dimensional array?

Hello, dear forum users!

I am unable to complete the task: You need to send a query to the database, given a two-dimensional array.

There is such an array ($activeAttr):

array:2 [▼
   "age" => array:2 [▼
     0 => "adults"
     1 => "children"
   ]
   "color" => array:1 [▼
     2 => "black"
   ]
]
public static function getAvailableAttributes ($categoryTitle, $allAttributes, $activeAttr) {
        return static::select($allAttributes)
            ->where(function ($query) use ($categoryTitle) {
                foreach ($categoryTitle as $item) {
                    $query->orWhere('category_title', $item);
                }
            })
            ->where(function ($query) use ($activeAttr) {
                foreach ($activeAttr as $item => $content) {
                    foreach ($content as $id => $attr) {
                        $query->orWhere($item, $attr);
                    }
                }
            })
            ->get();
    }

As a result, I get this request:

select `attribute_name`, `title` from `attributes` where (`attribute_name` = 'age' and `name` = 'adults' or `attribute_name` = 'age' and `name` = 'children' or `attribute_name` = 'color' and `name` = 'black') order by `title` asc

But I need a different type. All received attributes must be separated by "and", and all values of each attribute must be separated by "or" Something like this:

select `attribute_name`, `title` from `attributes` where (`attribute_name` = 'age' and `name` = 'adults' or `attribute_name` = 'age' and `name` = 'children' ) and ( `attribute_name` = 'color' and `name` = 'black') order by `title` asc

How can I implement this? Thanks in advance.

0 likes
4 replies
MichalOravec's avatar
Level 75
->where(function ($query) use ($activeAttr) {
    foreach ($activeAttr as $attributeName => $names) {
        $query->where(function ($query) use ($attributeName, $names) {
            $query->where('attribute_name', $attributeName)
                ->whereIn('name', $names);
        });
    }
})
1 like
Rediska's avatar

@MichalOravec Thank you! But for some reason it's an error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attribute_name' in 'where clause' (SQL: select `age`, `gender`, `color`, `brand` from `products` where (`stock` = in-stock) and (`category_title` = t-shirts or `category_title` = polo) and ((`attribute_name` = age and `name` in (adults, children)) and (`attribute_name` = color and `name` in (black))))
Rediska's avatar

@MichalOravec I did so and everything worked) Thank you very much!

->where(function ($query) use ($activeAttr) {
                foreach ($activeAttr as $attributeName => $names) {
                    $query->where(function ($query) use ($attributeName, $names) {
                        $query->whereIn($attributeName, $names);
                    });
                }
            })

Please or to participate in this conversation.