php 内爆 (101) 带引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6102398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php implode (101) with quotes
提问by mcgrailm
Imploding a simple array
内爆一个简单的数组
would look like this
看起来像这样
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
and that would return this
这将返回这个
lastname,email,phone
great, so i might do this instead
太好了,所以我可能会这样做
$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
and now i have what I want a nice pretty csv string
现在我有了我想要的漂亮漂亮的 csv 字符串
'lastname','email','phone'
is there a better way to do this, it feels to me like there should be an optional parameter for implode am I missing something ?
有没有更好的方法来做到这一点,我觉得应该有一个可选参数用于内爆我是否遗漏了什么?
采纳答案by Rafe Kettler
No, the way that you're doing it is just fine. implode()
only takes 1-2 parameters (if you just supply an array, it joins the pieces by an empty string).
不,你这样做的方式很好。implode()
只需要 1-2 个参数(如果你只提供一个数组,它会通过一个空字符串连接各个部分)。
回答by Umesh Moghariya
$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", $array) . "'";
回答by Felix Kling
You could use array_map()
:
你可以使用array_map()
:
function add_quotes($str) {
return sprintf("'%s'", $str);
}
$csv = implode(',', array_map('add_quotes', $array));
Also note that there is fputcsv
if you want to write to a file.
另请注意,fputcsv
如果您想写入文件。
回答by Robb
$ids = sprintf("'%s'", implode("','", $ids ) );
回答by Drew Dello Stritto
Don't know if it's quicker, but, you could save a line of code with your method:
不知道它是否更快,但是,您可以使用您的方法保存一行代码:
From
从
$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
To:
到:
$array = array('lastname', 'email', 'phone');
$comma_separated = "'".implode("','", $array)."'";
回答by Naftali aka Neal
If you want to use loops you can also do:
如果你想使用循环,你也可以这样做:
$array = array('lastname', 'email', 'phone');
foreach($array as &$value){
$value = "'$value'";
}
$comma_separated = implode(",", $array);
回答by Naftali aka Neal
Alternatively you can create such a function:
或者,您可以创建这样一个函数:
function implode_with_quotes(array $data)
{
return sprintf("'%s'", implode("', '", $data));
}
回答by doublejosh
If you want to avoid the fopen/fputcsv sub-systems here's a snippet that builds an escaped CSV string from an associative array....
如果你想避免 fopen/fputcsv 子系统,这里有一个片段,它从关联数组构建一个转义的 CSV 字符串......
$output = '';
foreach ($list as $row) {
$output .= '"' . implode('", "', array_values($row)) . '"' . "\r\n";
}
Or from a list of objects...
或者从对象列表...
foreach ($list as $obj) {
$output .= '"' . implode('", "', array_values((array) $obj)) . '"' . "\r\n";
}
Then you can output the string as desired.
然后您可以根据需要输出字符串。
回答by Jeremy French
Another possible option, depending on what you need the array for:
另一种可能的选择,取决于您需要阵列的用途:
$array = array('lastname', 'email', 'phone');
echo json_encode($array);
This will put '[' and ']' around the string, which you may or may not want.
这将在字符串周围放置 '[' 和 ']',您可能需要也可能不需要。
回答by rack_nilesh
I think this is what you are trying to do
我认为这就是你想要做的
$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", explode(',', $array)) . "'";