wwfc_barmy_army Posted May 18, 2010 Share Posted May 18, 2010 Hey Guys, I've got this code: while($row = mysql_fetch_array($result)) { $tagstringarray[]=$row['mycolumn']; } } foreach ($tagstringarray as $tag) { $pieces[] = explode(",", $tag); } foreach ($pieces as $tag) { echo $tag . "<br/>"; } print_r ($pieces); My database has a number of records which are tags, so are seperated by commas. eg. "tag1, tag2, tag3", but some just contain 1 tag eg "tag1". This seems to put it as (presumably) a multi dimensional array, any ideas how I can return it all to one single dimensional array? Thanks. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 Opposite of explosion is implosion implode Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted May 18, 2010 Author Share Posted May 18, 2010 I'm getting error: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\myfile.php on line 14 just swapped explode for implode. Any ideas? Thanks. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 Why would you swap one for the other? If you don't wan't to explode this string, just don't use explode... while($row = mysql_fetch_array($result)) { $tagstringarray[]=$row['mycolumn']; } foreach ($tagstringarray as $tag) { echo $tag."<br/>"; } Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted May 18, 2010 Author Share Posted May 18, 2010 the record contains either "tag1" or multiple tags "tag1, tag2, tag3". I want it to get all the individual tags and put them into an array. See what i mean? Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 That's what your original code does... Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted May 18, 2010 Author Share Posted May 18, 2010 It produces a multidimenstional array rather than a single array Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 How about: $pieces = array(); foreach ($tagstringarray as $tag) { $pieces = array_merge($pieces,explode(",", $tag)); } var_dump($pieces); Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted May 19, 2010 Author Share Posted May 19, 2010 That got it. Cheers. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 19, 2010 Share Posted May 19, 2010 $tagString = ''; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $tags = explode(',', $row['mycolumn']); $tagString .= implode('<br />', $tags) . '<br />'; } echo $tagString; var_dump($tagString); That work ok? 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.