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. Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/ Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 Opposite of explosion is implosion implode Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060029 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. Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060032 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/>"; } Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060036 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? Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060037 Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 That's what your original code does... Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060038 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 Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060041 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); Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060049 Share on other sites More sharing options...
wwfc_barmy_army Posted May 19, 2010 Author Share Posted May 19, 2010 That got it. Cheers. Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060416 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? Link to comment https://forums.phpfreaks.com/topic/202147-explode-spilt-arrays/#findComment-1060421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.