I'm confused about an error I am getting stating 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?
我感到困惑的原因是,我正试图这么做,将数组转换成字符串,使用内爆,根据手册,内爆应该允许我将数组转换成字符串。为什么会有误差呢?
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:
输出简单的数组并给出:
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?
内爆的手动状态——连接数组元素和字符串,为什么当我尝试去做的时候会出现错误?
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 ) )
4 个解决方案
#1
22
You have an array of arrays... Try this:
你有一个数组…试试这个:
$error_c = implode(',', $matches[0]);
#2
7
$error_c = implode(',', $matches[0]);
echo $error_c;
because your array
contains arrays
inside
因为数组中包含数组
#3
1
Do that:
这样做:
print_r($matches); // prints the array correctly
$error_c = implode(',', $matches[0]);
echo $error_c;
#4
0
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
http://php.net/manual/en/function.ob-get-clean.php
http://php.net/manual/en/function.ob-get-clean.php
#1
22
You have an array of arrays... Try this:
你有一个数组…试试这个:
$error_c = implode(',', $matches[0]);
#2
7
$error_c = implode(',', $matches[0]);
echo $error_c;
because your array
contains arrays
inside
因为数组中包含数组
#3
1
Do that:
这样做:
print_r($matches); // prints the array correctly
$error_c = implode(',', $matches[0]);
echo $error_c;
#4
0
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
http://php.net/manual/en/function.ob-get-clean.php
http://php.net/manual/en/function.ob-get-clean.php