[英]match all values in an array php
用户可以使用选择和复选框形式搜索某些内容。 数据在 GET 变量中发送。 我收集了变量中所有可能的值并将其放入一个数组中: $term_taxomony_ids_array = array($term_taxonomy_id_m, $term_taxonomy_id_l, $term_taxonomy_id_t_1, $term_taxonomy_id_t_2, $term_taxonomy_id_t_3, $term_taxonomy_id_t_4, $term_taxonomy_id_t_5, $term_taxonomy_id_t_6, $term_taxonomy_id_t_7, $term_taxonomy_id_t_8);
打印_r($term_taxomony_ids_array); 然后会给例如:
Array (
[0] => 12
[1] => 14
[2] =>
[3] =>
[4] => 9
[5] =>
[6] => 2
[7] =>
[8] =>
[9] =>
)
我如何使这个数组更简单但完全忽略空结果(如评论中所建议的那样)? 我需要在数据库中找到符合所有选择条件的“地点”。 我的数据库表已设置,所以我有两列。 1. Object_id 2. term_taxonomy_id。 term_taxonomy_id 是数组中的值。 例如我的桌子看起来像这样
Object id term_taxonomy_id
2 12
2 3
3 12
3 14
3 9
3 2
4 5
5 9
所以只有 object_id '3' 匹配数组中的所有术语 - 12, 14, 9, 2 我将如何运行查询来找到这个结果? 我正在使用一个 mysql 数据库,phpmyadmin,我的网站是建立在 wordpress 上的。 谢谢
基本上:
SELECT objectID, COUNT(term_taxonomy_id) AS cnt
FROM yourtable
WHERE term_taxonomy_id IN (2, 9, 14, 12)
GROUP BY objectID
HAVING cnt = 4
查找具有一个或多个匹配分类法 ID 的所有对象 ID,然后仅返回具有四个匹配分类法 ID 的对象 ID。
使用 IN,结合 COUNT() 和具有/GROUP。
$array = array_filter(array_unique($array));
$count = count($array);
$sql = "SELECT id, COUNT(*) FROM table WHERE field IN (" . implode(',', $array) . ") GROUP BY id HAVING COUNT(*) = " . $count;
SQL 可能有点偏离(您可能需要重新排序具有和组)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.