2

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!

2
  • I don't understand. Please make it clear, what is the input? What is the desired output? What are you getting differently? Commented Dec 4, 2012 at 16:36
  • well for starters, you aren't using array_push correctly. The first arg is the array and the 2nd arg is the value. 2nd, the returned value is the new number of elements in the array (basically a count($array). If you just want an array of user ids, see xbonez answer below Commented Dec 4, 2012 at 16:39

2 Answers 2

1

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'];  
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I do like this $users_id[] = $record['user_id']; $uid = array_push($users_id, ','); var_dump($uid); it show me like this int(2) int(4) int(6)
0
// 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 .= ")";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.