将值存储在cookie中

I have a two step question.

First I need to check if a particular numeric value is already in my string that is stored in a cookie and if not -- add to it.

$newVal = 4;
$oldStr = $_COOKIE['myCookie']; // get previous values (##, ###, ### ...)

if (!in_array($newVal, explode(',', $oldStr ))) {
    $newStr = $oldStr.', '.$newVal;
}
setcookie('myCookie', $newStr, time()+3600*24*30*12);

Later, on a different page, I need to compare values stored in my cookie against a given value from my db, and if at least one is not in that sting I need to add it to it.

$dbStr = '10, 20, 30, 40';    
// compare $dbStr against $_COOKIE['myCookie']
// updated will be $dbStr = '10, 20, 30, 40, 4';  

Now, here I'm really not sure how to compare the sting in cookie with $dbStr... Just having one of those blank moments.

I think:

$dbStr = '10, 20, 30, 40'; 
$curCookie = explode(', ', $_COOKIE['myCookie'] );
$dbValues = explode(', ', $dbStr); 
foreach ($curCookie as $value) {
   if(!in_array($value, $dbValues)
      $dbValues[] = $value
}
$dbStr = implode(', ', $dbValues);