我有一个简单的 PHP 数组,比方说
[1, 2, 4, 5, 6, 7]
我想查询一个MYSQL数据库
[1, who]
[2, where]
[3, some]
[6, there]
[9, too]
我只希望接收 PHP 数组和数据库的索引列之间的交集行。所以这会导致
[1, who]
[2, where]
[6, there]
有什么想法吗?谢谢!
最佳答案
你想要 sql in
关键字
SELECT id, title FROM tablename WHERE id IN (1, 2, 4, 5, 6, 7)
您可以使用以下方法准备数字列表:
$nums = array(1, 2, 4, 5, 6, 7)
$sql = 'SELECT id, title FROM tablename WHERE id IN (' . implode(',', $nums) . ')';
编辑:您可以使用 array_filter
确保您的输入仅包含数字:
$nums = array_filter($nums, 'is_numeric');
关于php - array_intersect_key PHP 数组与 MYSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2148330/