In PHP, I have applied this code to add a comma after string in a loop.
<?php $roomNames = ''; if(!empty($data['room_id'])) { $room_ids = explode(',', $data['room_id']); foreach($room_ids as $k=>$v) { $roomDetails = $this->db->select('room_name')->from('room')->where('room_id', $v)->get()->result_array(); if(sizeof($roomDetails) > 0) { $roomNames.=$roomDetails[0]['room_name'].', '; } } } echo $roomNames; ?>
I got this output Room 05, Room 06,
I want to remove the last comma from my string. How can I do it in PHP?
Apply this code
<?php $roomNames = []; if(!empty($data['room_id'])) { $room_ids = explode(',', $data['room_id']); foreach($room_ids as $k=>$v) { $roomDetails = $this->db->select('room_name')->from('room')->where('room_id', $v)->get()->result_array(); if(sizeof($roomDetails) > 0) { $roomNames[]=$roomDetails[0]['room_name']; } } } echo implode(', ', $roomNames); ?>
Output: Room 05, Room 06
Hope it will be helpful for you.
Question and answer is powered by AnsPress.io