Deleteing Part Of A Cookie Value

I currently store a list of table primary keys in a cookie for users that are not logged in. I can add to it with not problem but when I try to delete, under certain situation multiple values are being deleted.

An example, if the cookie stores the value "-1-2-3-4-5-6" and I delete "6" so the value is "-1-2-3-4-5-" then I delete "5", "1" will also be deleted.

My code for deleting is below


$value = isset(Yii::app()->request->cookies['wrap_crcpd_wishlist']) ? Yii::app()->request->cookies['wrap_crcpd_wishlist']->value : '';

                    if($value != ''){

                        $value = str_replace($id, '', $value);

                        

                        $value = str_replace('--', '-', $value);

                        Yii::app()->request->cookies['wrap_crcpd_wishlist'] = new CHttpCookie('wrap_crcpd_wishlist', $value);

                        echo "wishlist = {'id':'".$id."', 'message':'product deleted from wishlist', 'count':'".Yii::app()->wishlistChcker->wishlistNumber()."'}";

                    }

How can I stop this from happening?

Thank you

Found solution moments after.

Strange how writing the problems helps you understand and then solve it yourself.

I needed to add “$value = rtrim($value, ‘-’);” to the code so it looks like.


$value = str_replace($_GET[$i], '', $value);

                        $value = rtrim($value, '-');

                        $value = str_replace('--', '-', $value);