如何根据特定类别对数组值进行分组

发布于 2024-12-14 21:14:06 字数 1145 浏览 0 评论 0原文

多个选择选项的数组输出

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-12-21 21:14:06

您可以使用 PHP 关联数组来存储查询,如下所示:

$inArray['animals'] = SELECT * FROM kids WHERE 
                      (animals="monkey" OR animals="pig" OR animals="dog")

$inArray['fruits']  = SELECT * FROM kids WHERE
                      (fruits="banana" OR fruits="apple" OR fruits="mango") 

$inArray['flowers'] = SELECT * FROM kids WHERE
                     (flowers="lavender" OR flowers="magnolia" OR flowers="marigold")

You can use PHP associative array to store the query like this:

$inArray['animals'] = SELECT * FROM kids WHERE 
                      (animals="monkey" OR animals="pig" OR animals="dog")

$inArray['fruits']  = SELECT * FROM kids WHERE
                      (fruits="banana" OR fruits="apple" OR fruits="mango") 

$inArray['flowers'] = SELECT * FROM kids WHERE
                     (flowers="lavender" OR flowers="magnolia" OR flowers="marigold")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文