-4

I am going to separate an array's elements with , but I have a error

Array to string conversion

My php codes:

// 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

What is wrong? Thank you.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88
  • 1
    shouldn't you imploding `$data['my_slider']` instead? – Kevin Mar 17 '15 at 10:48
  • Put your error into Stack search, or Google "Array to string conversion" - what happens? ;) It sometimes (usually) takes less time to search an error and read the results than it does asking a question. – James Mar 17 '15 at 10:49
  • 2
    why do you have brackets after $filename_arr – alex9311 Mar 17 '15 at 10:49

3 Answers3

2

Try removing the brackets from the variable filename_arr.

// Use $data['my_slider']
$filename_arr = $data['my_slider'];
$file_coma = implode(',', $filename_arr);
Silfverstrom
  • 28,292
  • 6
  • 45
  • 57
0

You appear to have all sorts of unnecessary code in your example and you dont seem to understand arrays very well.

$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;

The variable $file_coma should now contain a comma seperated list of whatever was in $row->pic.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thank you very much. But I need $data['slider'] to pass into view. There was just a mistake That was solved in first answer. I don't know why I got 4 negetive! Any way, Thanks :) – Vahid Najafi Mar 17 '15 at 10:59
  • 1
    @vahidnajafi "don't know why I got 4 negative" because Stack and the entire internet would have told you what your issue is had you bothered to even try searching first. Sorry to be blunt, but this question is pointless. It was even a waste of your time, as you could have found your answer quicker by searching. – James Mar 17 '15 at 11:01
  • Ok, so I changed the code so it now adds the array of pics into a variable called `$data['slider']` for your view. – RiggsFolly Mar 17 '15 at 11:03
  • 1
    @vahidnajafi No need for apology, was just giving advice. – James Mar 17 '15 at 11:18
-1
$data = ['first'=>'One','sec'=>'Two','third'=>Three];
echo implode(' ',$data);

The output is

One Two Three
Talha
  • 1
  • 1