Darkmatter5 Posted January 8, 2009 Share Posted January 8, 2009 I have an array value that is 1,2,14. Then I have code that will split those values using "," as the seperator and places the values into another array. Then I have code that gets a string based on that those values and outputs that string to the page, but I really want to take that string to be placed in another array and seperate each new string with ", ". How can I do this? I'm basically wanting to convert "1,2,14" into "apples, oranges, lemons" and apply it to the original array $row['fruits']. You'll see below. <?php $result=mysql_query($query) or die(mysql_error()); while($row=mysql_fetch_array($result)) { $fru_ids=explode(",",$row['fru_id']); foreach($fru_ids as $fru_id) { $query1="SELECT name FROM fruits WHERE fruit_id=$fru_id"; $result2=mysql_query($query1) or die(mysql_error()); $fru=mysql_fetch_array($result2); echo $fru['name']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140056-constructing-a-string-to-be-placed-into-an-array/ Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 <?php $result=mysql_query($query) or die(mysql_error()); while($row=mysql_fetch_array($result)) { $fru_ids=explode(",",$row['fru_id']); $query1="SELECT name FROM fruits WHERE fruit_id IN($fru_id) ORDER BY fruit_id"; $result2=mysql_query($query1) or die(mysql_error()); $fruits = array(); // reset array. while ($row2 = mysql_fetch_assoc($result2)) { $fruits[] = $row2['name']; } $row['fruits'] = implode(", ", $fruits); } ?> I think that is what you want. Quote Link to comment https://forums.phpfreaks.com/topic/140056-constructing-a-string-to-be-placed-into-an-array/#findComment-732799 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.