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

Author: Zeeshan Arshad     2807     02/13/2013

Estimated reading time: 0 minutes, 27 seconds

Zeeshan Arshad - Lifestyle Blog

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)"

 

 ?>

« Return to Blog