0

I am having an array which is also having some blank space and because of those blank spaces, when I imploding the array it is giving me extra comma. Please take a look at my code and suggest me how can I get rid of it.

$xrd = implode(',', $admin_array);}

//Output is ,12,62,,76,,,45,,
//Output should be 12,62,76,45

1
  • 2
    Could you please add little bit more code what elments are in $admin_array? Commented Sep 25, 2021 at 16:02

1 Answer 1

1

Remove the blank elements before imploding.

$xrd = implode(',', array_filter($admin_array));

If the array could contain the string 0, you'll need to be more specific:

$xrd = implode(',', array_filter($admin_array, function($x) { return $x !== ""; });
Sign up to request clarification or add additional context in comments.

1 Comment

Just be careful as this will remove 0 as well which maybe you want to keep.

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.