2014-09-19 111 views
9

我感到困惑的錯誤我得到說明Array to string conversion數組字符串轉換錯誤使用破滅

的原因,我很困惑就是我想要做到這些,轉換數組爲字符串時,使用implode,根據手冊應該允許我將我的數組轉換爲字符串。那麼,爲什麼我會得到一個錯誤?

var $matches是一個數組。 $error_c是我想要存儲字符串的變量。

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)) 
+0

你的數組的元素應該是字符串或具有'__toString'能力 – 2014-09-19 08:43:15

+0

你明白了!檢查答案;) – lpg 2014-09-19 08:44:24

+0

$ matches是一個數組。那麼怎麼能爆破整個數組$匹配而不爆炸它,如果沒有,那麼你不得不提及索引數組像$匹配[0] – 2014-09-19 09:13:23

回答

20

讓你有一個數組的數組...試試這個:

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

這樣做:

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

因爲你的array包含arrays裏面