The below code is working correctly, however, I am not sure if the combination of while
, foreach
and implode
is really necessary here or if the same can be achieved much simpler.
What I do here is fetching some data (text) from a db and the only thing I need is the results as a comma separated string.
Now even though this is working I was wondering if there is a direct way to get the results as a comma separated string since the arrays here are not needed for anything else.
$stmt = $conn->prepare("SELECT " . $languageFrm . " FROM TranslationsMain WHERE location LIKE ? ORDER BY sortOrder, " . $languageFrm);
$stmt->bind_param("s", $location);
$stmt->execute();
$result = $stmt->get_result();
$strTranslations = array();
while($arrTranslations = $result->fetch_assoc()){
$translations[] = $arrTranslations;
}
foreach($translations as $translation){
array_push($strTranslations, $translation[$languageFrm]);
}
echo implode(",", $strTranslations);