Looks like you're constructing a WHERE
clause to an SQL query. Here's a clue on how to build the string, but you should be using prepared statements instead of injecting user input directly into your query! Also, you don't actually need to nest a foreach, as is demonstrated below. See comments for explanation.
To use prepared statements (and please do!), you can substitute the variables in the AND
ed conditions with "product.? ? ?";
. Then use PDOStatement::bindValue(). Other flavors of parameter binding are available.
<?php
// Your input array.
$filters = [["category_id","=","3"],["price","<","40"]];
// Your question is unclear on this, but if your $_POST['filter'] is not actually an array, but a JSON string, then you may try this:
$filters = json_decode($_POST['filter'], TRUE); // TRUE to decode to associative array.
// $where holds each WHERE condition, so you can perform a clean string glue with implode().
// In your current string concatenation, additional conditions are required, or the query
// will fail due to an errant trailing AND if none are following.
$where = [];
// $filterText will hold this part of your WHERE clause. Consider renaming it to something
// more descriptive, like $whereSql.
$filterText = '';
foreach ($filters as [$key, $comparisonOperator, $value])
$where[] = "product.$key $comparisonOperator $value";
// Glue each condition with as many ANDs are necessary.
$filterText .= implode(' AND ', $where);
echo $filterText;
// Outputs: product.category_id = 3 AND product.price < 40
UPDATE: Your question is a little unclear on this, so I've added something that you can try. This updated answer now assumes your input isn't actually an array, but a JSON string.