1

I have a simple PHP loop that iterates over a column in my table of trainers.

The data that it is getting is their first and last name. I am trying to remove the ending comma in the string once there are no more results in the iteration but it does not seem to work.

// Get the trainers
foreach ($data->trainers as $trainers) {
  $trainer .= (string)$trainers->trainer->trainerFirst. ' ' . (string)$trainers->trainer->trainerLast.',';
}
// Trim the trailing comma
rtrim($trainer, ",");

I would have expected this to work but the output is always First Last, instead of First Last.

Any ideas on what I am doing wrong?

1 Answer 1

7

rtrim doesn't modify the string in place, it returns a new string, so you need to assign the return value. Try this:

// Trim the trailing comma
$trainer = rtrim($trainer, ",");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.