<?
$author = $row['author'];
$publisher = $row['publisher'];
$arr = array($author,$publisher);
$imploded = implode(", ", $arr);
echo $imploded;
?>
In the code above, I'm getting author and publisher information from a database, but I want to combine them (comma separated).
They are not required fields. So, some rows will have both, or only one. However, PHP is still printing the comma if both fields are empty, OR if only one field has data. So, the output often looks like this:
row1: Author,
row2: Author, Publisher <--correct
row3: ,
row4: , Publisher
Ideally, the output should look like this:
row1: Author
row2: Author, Publisher
row3:
row4: Publisher
Why is my array printing like this? How can I fix it without adding count($arr) and building IF statements? There must be a simple solution...