1

I have this 3 optional input text which is not required at all but at least one should be filled.

<input type="text" name="text1" class="form-control">
<input type="text" name="text2" class="form-control">
<input type="text" name="text3" class="form-control">

and I put their values in array like this

$p =array{
    $this->input->post('text1'),
    $this->input->post('text2'),
    $this->input->post('text3')
}

If I only filled 2 fields, the result is like this:

Array
(
    [0] => John
    [1] => Jack
    [2] => 
)

I implode them:

$passenger = implode(',', $p);

The result:

John, Jack,

What I want is:

John, Jack

How to eliminate the last ',' using PHP? Thank You

2 Answers 2

3

Use array_filter to remove the empty entries from your array before imploding it.

$passenger = implode(',', array_filter($p));
Sign up to request clarification or add additional context in comments.

Comments

0

you should be use trim function.

$passenger = trim(implode(',', $p),",");

or

$passenger = implode(',', $p);
$passenger = trim($passenger,",");

Also In Case of This Array

Array
(
    [1] => Jack
    [2] => 
    [3] => ,
)

Trim Work Perfectly.

$passenger = trim(implode(',', $p),",");

Output: Jack

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.