1

PHP i am getting this:

array(2) { 
  [0]=> array(1) 
   { 
     ["username"]=> string(7) "9994344" 
   } 
  [1]=> array(1)
   {
     ["username"]=> string(7) "9994345" 
   } 
 }

How to make that into comma separate list

ex. SELECT *from table1 WHERE column IN ("9994344","9994345")

1
  • foreach loop but its slow. Commented Feb 3, 2014 at 5:33

3 Answers 3

3
$in_list = implode(',', array_map(function($x) {
    return '"' . $x['username'] . '"';
}, $array));

$sql = "SELECT * FROM table1 WHERE column IN ($in_list)";
Sign up to request clarification or add additional context in comments.

1 Comment

I like the use of array_map();
1
$ids=array(); 
while($row = mysql_fetch_assoc($result))
{
  $ids[]=$row["UserID"]; 
} 
echo implode(", ", $ids);

1 Comment

You're missing the quotes around the IDs.
1

try this

$arr_username = array();
foreach($arr as $arr_value)
{
    $arr_username[] = '"'.$arr_value['username'].'"';
}

$in = implode(",", $arr_username);

$sql = "SELECT * FROM table1 WHERE column IN ($in)";

3 Comments

You're missing the quotes around the usernames.
@Barmar i am updating the answer. whereas it can be work because username is a number. what you thing ?
Now it's essentially the same as my answer, except I use array_map instead of a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.