Akaash
Akaash

Reputation: 77

Need to push values of an array into a variable as a string

Need to push values of an array as like ('1','2','3'........) in a $variable. I have a while loop from where I am getting the values 1,2,3.... I am doing like this

while ($record = mysql_fetch_array($query, MYSQL_ASSOC)) {  
    $users_id = $record['user_id'];  
    $uid = array_push($users_id, ','); 
}

I need these values as a String in a variable, then I will use explode function to remove ',' and use it according to my need. Please anyone can help me in this.. Thanks!

Upvotes: 2

Views: 124

Answers (2)

SSH This
SSH This

Reputation: 1929

// open parens
$newString = "(";

// for each value add quotes and comma
foreach($users_id as $v) 
   $newString .= "'".$v."',";

// remove the trailing comma
$newString = substr($newString , 0, -1); 

// close parens
$newString .= ")";

Upvotes: 0

Ayush
Ayush

Reputation: 42450

Adding them to a string and exploding it will give you an array of the values. You can just directly push them to an array

$users_id = array();    

while ($record = mysql_fetch_array($query, MYSQL_ASSOC)) {  
    $users_id[] = $record['user_id'];  
}

Upvotes: 1

Related Questions