PHP: Convert Comma Separated String into Array or Array into String

Author: Zeeshan Arshad     2364     02/13/2013

Estimated reading time: 0 minutes, 27 seconds

Convert Comma Separated String into Array

 

$str_numbers = "1, 2, 3, 4, 5";

 

// convert into array

 

$id_nums = array($str_numbers);

 

?>

 

Convert an Array into Comma Separated String

 

$id_nums = array(1,2,3,4,5);

 

// convert an array into comma separated string

 

$id_nums = implode(", ", $id_nums); 

 

// it's usage example in a query; 

 

$query = "SELECT name, email FROM users WHERE user_id IN ($id_nums)";

 

// $query becomes "SELECT name, email FROM users WHERE user_id IN (1,2,3,4,5)"

 

 ?>


Was this topic helpful?

Yes
No
Subscribe YouTube Channel

Zeeshan Arshad is a self-taught and multi-dimensional person with extraordinary track record of success with international clients and students.

Feel free to contact him for your project, training purpose or guidance.