1

I have an array that looks like this:

Array
(
    [0] => 41
    [1] => 43
    [2] => 44
    [comment] => 
)

is there any way to implode this array ignoring ['comment']??

Now ['comment']doesn't has content but sometimes it can have content. I need to ignore ['comment'] always.

Also ['comment'] will be always the last array.

0

2 Answers 2

2

Simply use unset and implode

unset($arr['comment']);
echo implode(',',$arr);

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

smart solution, I've fought with this all day :(
1

Use a negative offset with array_splice to remove the last element of the array.

$string = implode(',', array_splice($array, -1));

2 Comments

My bad, must be splice. Thank you for noticing. :)
it works because comment is the last index present in the array :)

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.