-1

I have tried most of the solutions I found, but cannot get it to work.

I have a form parameter that post list of ids separated with commas in a string as per

$list_of_ids = "261,420,788";

Now I need to convert the list of ids into object or arrays so that I can loop through it and insert into database.

My problem is how I can make it look like this:

["261","420","788"]

Here is how I will like to loop it and update database

foreach($list as $id){

echo $id;
// loop and update database


}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38

2 Answers2

1
$list = explode(",", $list_of_ids);

Should give you an array of the numbers. Take precaution against sql-injections though, if you want to put this into a database!

Daniel
  • 426
  • 3
  • 14
0

Just use explode :

$string= "123,234,345";
$array = explode(",",$string);

Output :

Array ( [0] => 123 [1] => 234 [2] => 345 )

leobrl
  • 959
  • 1
  • 6
  • 15