使用implode时,数组到字符串转换错误

问题描述 投票:17回答:5

我很困惑我正在陈述Array to string conversion的错误

我很困惑的原因是我正在尝试这样做,将数组转换为字符串,使用implode,根据手册应该允许我将我的数组转换为字符串。那我为什么会收到错误?

var $matches是一个数组。 $error_c是我想要存储字符串的var。

print_r($matches); // prints the array correctly
$error_c = implode(',', $matches);
echo $error_c;

输出只是array并给出:

Notice: Array to string conversion in ...

手册说implode — Join array elements with a string所以当我尝试这样做时为什么会出错?

编辑:这是我从$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 ) ) 
php arrays string implode
5个回答
29
投票

你有一个数组数组...试试这个:

$error_c = implode(',', $matches[0]);

8
投票
$error_c = implode(',', $matches[0]);
echo $error_c;

因为你的array里面有arrays


1
投票

去做:

print_r($matches); // prints the array correctly
$error_c = implode(',', $matches[0]);
echo $error_c;

0
投票

要将数组中的任何数据放入字符串中,请尝试此操作

function whatever_to_string($in){
    ob_start();
    print_r($in);
    return ob_get_clean();
    }

'ob_ *'函数控制输出缓冲区。

http://php.net/manual/en/function.ob-start.php

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


0
投票

您可以将array_values()用于数组数组

例如implode (",", array_values($array))

© www.soinside.com 2019 - 2024. All rights reserved.