我需要从数据库中打印标记ID,如下所示:'1','2','3'
。我有这段代码:
$_POST['tags'] = "1,2,3";
$arr_tag = explode(",", $_POST['tags']);
$arr_tag = str_replace(' ', '-', $arr_tag);
foreach($arr_tag as $tag)
{
$__SQL__ = data::FETCH("SELECT id FROM " . TAGS . " WHERE name = ?", $tag);
$tags_id[] = $__SQL__[0]['id'];
$quoted_tags = array_map(function ($x){ return "'$x'";}, $tags_id);
$string = implode(',', $quoted_tags);
echo $string;
}
输出是:
'126''126','303''126','303','308'
在行动中$_POST['tags'] = "1,2,3";
有3个数组值但在输出中我看到6个值:'126''126','303''126','303','308'
。
我该如何解决这个问题?
答案 0 :(得分:1)
问题是你在循环中做array_map
和implode
。所以每次你看到结果的运行列表。循环完成后,你应该只做一次:
foreach($arr_tag as $tag)
{
$__SQL__ = data::FETCH("SELECT id FROM " . TAGS . " WHERE name = ?", $tag);
$tags_id[] = $__SQL__[0]['id'];
}
$quoted_tags = array_map(function ($x){ return "'$x'";}, $tags_id);
$string = implode(',', $quoted_tags);
echo $string;