php 使用内爆时数组到字符串的转换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25929733/
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
Array to string conversion error when using implode
提问by Francesca
I'm confused about an error I am getting stating Array to string conversion
我对我所陈述的错误感到困惑 Array to string conversion
The reason I'm confused is I'm trying to do exactly that, convert an array to a string, using implode
which according to the manual should allow me to convert my array into a string. So why am I getting an error?
我感到困惑的原因是我正试图做到这一点,将数组转换为字符串,implode
根据手册使用它应该允许我将数组转换为字符串。那么为什么我会收到错误消息?
var $matches
is an array. $error_c
is the var I want to store the string.
var$matches
是一个数组。$error_c
是我要存储字符串的 var。
print_r($matches); // prints the array correctly
$error_c = implode(',', $matches);
echo $error_c;
Outputs simply array
and gives:
简单地输出array
并给出:
Notice: Array to string conversion in ...
The manual states that implode — Join array elements with a string
so why do I get an error when I try to do it?
手册指出,implode — Join array elements with a string
为什么我尝试这样做时会出错?
Edit: this is the output I get from $matches
编辑:这是我得到的输出 $matches
Array ( [0] => Array ( [0] => C [1] => E [2] => R [3] => R [4] => O [5] => R [6] => C [7] => O [8] => N [9] => T [10] => A [11] => C [12] => T [13] => S [14] => U [15] => P [16] => P [17] => R [18] => E [19] => S [20] => S [21] => E [22] => D ) )
回答by lpg
You have an array of arrays... Try this:
你有一个数组数组......试试这个:
$error_c = implode(',', $matches[0]);
回答by Mad Angle
$error_c = implode(',', $matches[0]);
echo $error_c;
because your array
contains arrays
inside
因为你array
包含arrays
在里面
回答by Enrique Mu?oz Rodas
Do that:
去做:
print_r($matches); // prints the array correctly
$error_c = implode(',', $matches[0]);
echo $error_c;
回答by Mike
You may use array_values()
for array of arrays
您可以array_values()
用于数组数组
e.g. implode (",", array_values($array))
例如 implode (",", array_values($array))
回答by jmullee
To just put whatever data is in the array into a string, try this
要将数组中的任何数据放入字符串中,试试这个
function whatever_to_string($in){
ob_start();
print_r($in);
return ob_get_clean();
}
The 'ob_*' functions control the output buffer.
'ob_*' 函数控制输出缓冲区。
http://php.net/manual/en/function.ob-start.php
http://php.net/manual/en/function.ob-start.php