php:数组到字符串转换。怎么解决这个?

时间:2015-03-17 10:45:50

标签: php arrays implode

我要将数组的元素与,分开但我有错误

Array to string conversion

我的PHP代码:

// get sliders from database
$all_slider = $this->db_submit_product->get_slider($shoe_ID);
$data['my_slider'] = array();

foreach ($all_slider as $row) {
    array_push($data['my_slider'], $row->pic);
}

// Use $data['my_slider']
$filename_arr[] = $data['my_slider'];
$file_coma = implode(',', $filename_arr);
// line of error is about last line

有什么问题?谢谢。

3 个答案:

答案 0 :(得分:1)

尝试从变量filename_arr中删除括号。

// Use $data['my_slider']
$filename_arr = $data['my_slider'];
$file_coma = implode(',', $filename_arr);

答案 1 :(得分:0)

您的示例中似乎有各种不必要的代码,您似乎不太了解数组。

$all_slider = $this->db_submit_product->get_slider($shoe_ID);
$pic_list = array();

foreach ($all_slider as $row) {
    $pic_list[] = $row->pic;
}

$file_coma = implode(',', $pic_list);

// put the pic_list array into $data for the View.
$data = array();
$data['slider'] = $pic_list;

变量$file_coma现在应该包含$row->pic中所有内容的逗号分隔列表。

答案 2 :(得分:-1)

$data = ['first'=>'One','sec'=>'Two','third'=>Three];
echo implode(' ',$data);

输出为

One Two Three