如何根据特定类别对数组值进行分组
多个选择选项的数组输出
Array
(
[q] => Array
(
[0] => monkey
[1] => pig
[2] => dog
[3] => banana
[4] => apple
[5] => mango
[6] => lavender
[7] => magnolia
[8] => marigold
)
)
代码
$queryArray = array_map("secure", $_GET['q']);
$inArray = array();
foreach ($queryArray as $key) {
$inArray[] = 'animals="' . $key . '"';
}
$sql = 'SELECT * FROM kids WHERE ' . implode(' AND ', $inArray) . '';
echo $sql;
问题:
如何将当前数组分组为
animals = monkey,pig,dog
fruits = banana,apple,mango
flowers = lavender,magnolia, marigold
可能手动过滤值,或者如果 in_array
monkey,pig,dog
是动物。
并做出正确的 MySQL 语句以获得类似这样的信息
SELECT *
FROM kids
WHERE
(animals="monkey" OR animals="pig" OR animals="dog")
AND
(fruits="banana" OR fruits="apple" OR fruits="mango")
AND
(flowers="lavender" OR flowers="magnolia" OR flowers="marigold")
让我知道。
Array output from multiple select option
Array
(
[q] => Array
(
[0] => monkey
[1] => pig
[2] => dog
[3] => banana
[4] => apple
[5] => mango
[6] => lavender
[7] => magnolia
[8] => marigold
)
)
The code
$queryArray = array_map("secure", $_GET['q']);
$inArray = array();
foreach ($queryArray as $key) {
$inArray[] = 'animals="' . $key . '"';
}
$sql = 'SELECT * FROM kids WHERE ' . implode(' AND ', $inArray) . '';
echo $sql;
Question :
How do I group the current array to be
animals = monkey,pig,dog
fruits = banana,apple,mango
flowers = lavender,magnolia, marigold
Maybe filter the value manually or if in_array
monkey,pig,dog
is animals.
and make a correct MySQL statement to get something like this
SELECT *
FROM kids
WHERE
(animals="monkey" OR animals="pig" OR animals="dog")
AND
(fruits="banana" OR fruits="apple" OR fruits="mango")
AND
(flowers="lavender" OR flowers="magnolia" OR flowers="marigold")
Let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PHP 关联数组来存储查询,如下所示:
You can use PHP associative array to store the query like this: