Northern Flame Posted February 27, 2008 Share Posted February 27, 2008 I have values from an array which are a numbered values. for example: $array = array(1, 45, 67, 22, 86); i want to put them into a column of my database like this: 1,45,67,22,86 just like that, all the numbers in the same column but separated by commas. how will i do that? will it be something like this: $sql = "UPDATE table SET numbers = '"; foreach($array as $val){ $sql .= "$val,"; } $sql .= "' WHERE id='$id'"; mysql_query($sql); is there a smarter way of doing this? or is that the correct way? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 27, 2008 Share Posted February 27, 2008 For a one dimensional array, use the implode() function: $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted February 27, 2008 Author Share Posted February 27, 2008 oh haha thanks! i totally forgot about the implode() function! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.