我关注了名为$user_id_arr
Array
(
[0] => 92ecd33db4ddcdc28e025cae80f00208
[1] => 9def02e6337b888d6dbe5617a172c18d
[2] => a6d22e4cc3f65778a60b359842bcec82
)
现在我想要一个包含上面数组值的字符串,但是每个数组值都应该用单引号括起来。为此,我尝试了以下代码,但没有成功。
$user_ids = implode(",", $user_id_arr);
我正在关注数组:
92ecd33db4ddcdc28e025cae80f00208,9def02e6337b888d6dbe5617a172c18d,a6d22e4cc3f65778a60b359842bcec82
有人可以帮我这方面吗?我想要以下格式的所需数组:
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
提前致谢。
答案 0 :(得分:2)
您可以使用implode
作为建议的上述答案。如果您想直接修改数组,那么可以使用array_walk()
。
<?php
$arr=Array
(
0 => '92ecd33db4ddcdc28e025cae80f00208',
1 => '9def02e6337b888d6dbe5617a172c18d',
2 => 'a6d22e4cc3f65778a60b359842bcec82',
);
array_walk($arr,function (&$val){ $val="'".$val."'";});
echo implode(',',$arr); //<----- This is the one you wanted by the way...
print_r($arr);
<强> OUTPUT :
强>
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Array
(
[0] => '92ecd33db4ddcdc28e025cae80f00208'
[1] => '9def02e6337b888d6dbe5617a172c18d'
[2] => 'a6d22e4cc3f65778a60b359842bcec82'
)
答案 1 :(得分:1)
$user_id_arr = array
(
'0' => '92ecd33db4ddcdc28e025cae80f00208',
'1' => '9def02e6337b888d6dbe5617a172c18d',
'2' => 'a6d22e4cc3f65778a60b359842bcec82'
);
$user_ids = "'". implode("','",$user_id_arr)."'";
echo $user_ids;
答案 2 :(得分:1)
答案 3 :(得分:0)
foreach($user_id_arr as $r){
$user_ids = $user_ids."'".$r."',";
}