php : 数组到字符串的转换.如何解决这个问题


php : Array to string conversion. How to solve this?

我要用,分隔数组的元素,但我有一个错误

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

怎么了?谢谢。

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

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

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

$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中任何内容的逗号分隔列表。

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

输出为

One Two Three