Jump to content

Implode failing, but why?


jfulbrook

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/181948-implode-failing-but-why/
Share on other sites

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.