jfulbrook Posted November 18, 2009 Share Posted November 18, 2009 Hello there, I am using an 'Implode' to separete my array with commas. Pretty standard I thought, but if fails every time, but why, I don't know? Any suggestions would be greatfully recieved, please see code below: $row_getJournalAuthors = mysql_fetch_array($query_getJournalAuthors); $arr = array($row_getJournalAuthors['name']); $str = implode(", ",$arr); echo $str; The result is just the names, but no commas or spaces; like this - John SmithJoe BloggsBurt Reynolds Any clues anyone? Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/ Share on other sites More sharing options...
Alex Posted November 18, 2009 Share Posted November 18, 2009 It's because implode() takes an array and separates each element and makes a string using the first parameter as a delimiter. In your code $arr only contains one element( $row_getJournalAuthors['name'] ), so nothing is actually done. Can you show us an example of what $row_getJournalAuthors['name'] contains? Quote Link to comment https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/#findComment-959721 Share on other sites More sharing options...
JasonLewis Posted November 18, 2009 Share Posted November 18, 2009 You'll need to explode() the variable at the spaces by the looks of it, then use implode. Or you could just use str_replace() on the spaces and put in commas. Quote Link to comment https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/#findComment-959725 Share on other sites More sharing options...
Alex Posted November 18, 2009 Share Posted November 18, 2009 Actually, judging by the output the OP stated there's no delimiters used in the database record. The spaces you're seeing are actually the spaces separating first and last names. Quote Link to comment https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/#findComment-959727 Share on other sites More sharing options...
jfulbrook Posted November 18, 2009 Author Share Posted November 18, 2009 Hello, Thanks for the response so far. The data for $row_getJournalAuthors['name'] is a first and second name in one field. i.e. John Smith. The other field is an author ID. When i use a for each loop like this: $row_getJournalAuthors = mysql_fetch_array($query_getJournalAuthors); foreach($row_getJournalAuthors as $author){ $arr[] = $author;} $str = implode(", ",$arr); echo $str; I get: 144, 144, Paul smith, Paul smith, 1, 1144, 144, Paul smith, Paul smith, 1, 1, 147, 147, Lynne marks, Lynne marks, 1, 1 Please note there is a While loop going on behind the for each loop Quote Link to comment https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/#findComment-959743 Share on other sites More sharing options...
9three Posted November 18, 2009 Share Posted November 18, 2009 I think you're over complicating the situation. Try this: $results = array(); while ($row = mysql_fetch_array($query_getJournalAuthors)) $results[] = $row['name']; echo implode(', ', $results); Quote Link to comment https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/#findComment-959762 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.