首页 > 解决方案 > 使用 PHP 如何将数组转换为用于 SQL 查询的项目字符串

问题描述

我需要使用循环获取我在数组中获取的大量数组,即

$uuids = [];
foreach($data AS $item) {
  $uuids[] = $item->uuid;
}

$uuids = implode(",", $uuids);

但是在这种方法中,我要返回

"123-asdf,122-asfdsa,2323-aewrq"

但是我需要

'123-asdf','122-asfdsa','2323-aewrq'

如果我将上述作为字符串连接进行,即

$uuids = "";
foreach($data AS $item) {
  $uuids .= "'$item->uuid',";
}

$uuids = implode(",", $uuids);

我明白了

'\'123-asdf\',\'122-asfdsa\',\'2323-aewrq\'

我又不能将其用于 SQL where 语句。请问有人有什么建议可以帮忙吗?

标签: phpmysqlarraysstring

解决方案


推荐阅读