-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
  • 18,701
  • 13
  • 79
  • 158
Nancy Mooree
  • 1,715
  • 1
  • 9
  • 20

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
  • 376
  • 3
  • 14
0

Just use explode :

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

Output :

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

leobrl
  • 877
  • 1
  • 4
  • 14