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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:03:18  来源:igfitidea点击:

Array to string conversion error when using implode

phparraysstringimplode

提问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 implodewhich according to the manual should allow me to convert my array into a string. So why am I getting an error?

我感到困惑的原因是我正试图做到这一点,将数组转换为字符串,implode根据手册使用它应该允许我将数组转换为字符串。那么为什么我会收到错误消息?

var $matchesis an array. $error_cis 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 arrayand gives:

简单地输出array并给出:

Notice: Array to string conversion in ...

The manual states that implode — Join array elements with a stringso 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 arraycontains arraysinside

因为你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

http://php.net/manual/en/function.ob-get-clean.php

http://php.net/manual/en/function.ob-get-clean.php