php - horizontal list and comma issue

i wanna thank you first of all
My problem is i have different city names and i wanna list them as horizontal with comma of course. Accualy i do that. but last city name has commo it has a little bit grammer issue as you know end of sentence does not end with a comma. So how can i delete comma for last city name ?
<?php
$max = 5;
$space =', ';
$taxonomy = 'sehir';
$terms = wp_get_object_terms($post->ID , 'sehir');
shuffle($terms);
$terms = array_slice($terms, 0, $max);
usort($terms, function($a, $b){
return strcasecmp($a->name, $b->name);
});
if ($terms) {
foreach($terms as $term) {
echo $term->name, $space;
}
}
?>
also i wanna selected by one by with a select option ? How can i do that ? Thx again
<div class="form-group col-md-6">
<label for="">KatД±lД±m NoktasД±</label>
<select name="" id="" class="form-control" required>
<option value="">A</option>
<?php foreach ($term as $datum): ?>
<option value=""><?php echo $term->name;?> </option> <!-- test here but doesnt work --->
<?php endforeach; ?>
<option value="">D noktasД±</option>
<option value="">E noktasД±</option>
<option value="">F noktasД±</option>
<option value="">G noktasД±</option>
</select>
</div>
List of cities with dropdown menГј but doesnt work :'(
Answer
Solution:
When you print, it should be $datum->name instead of $term->name https://www.php.net/manual/en/control-structures.foreach.php
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function illuminate\encryption\openssl_cipher_iv_length()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.