I am working on a project that requires imploding an array with character separation. I have successfully used join
and implode
interchangeably in other parts of the project, but I can't get it to work in this section.
$dbQuery = "SELECT ftc.*, fc.name
FROM facilities f
LEFT JOIN facility_to_category ftc ON f.fid = ftc.fid
LEFT JOIN facility_categories fc ON ftc.cid = fc.cid
WHERE f.listing_year = '2011'
AND fc.parent_cid = '2'
AND f.fid = ('".$listing_fid."')";
$dbResult = $dbc->query($dbQuery,__FILE__,__LINE__);
$num_results = $dbc->num_rows($dbResult);
echo '<h3>Demographics</h3>
<div>';
while($catdata = $dbc->fetch($dbResult)) {
$demographics = array();
$demographic_names = array('',trim($catdata->name));
$demographics = implode(' '.chr(149).' ',$demographic_names);
print $demographics;
}
The result is this:
Demographics
• Affluent • Children • Hard-to-Reach • Parents
instead of
Demographics
Affluent • Children • Hard-to-Reach • Parents
I've tried using double quotes instead of single quotes around '.chr(149).'. I've tried using commas or bars or just spaces. I've tried different ways of trimming and not trimming $catdata->name
.
I also thought about trying string concatenation, but then I'll end up with an extra character at the end instead of the beginning. Implode
or join
seem the better way to go.
What am I missing?
$demographic_names[0]
blank? Also, why are you setting$demographics = array();
then almost immediately turning it into a string? – Matt H Jan 24 '12 at 21:01