I have a column called "meta_value" in mysql table whos value is stored in comma seperated format "1,2,4". i have a variable $v=2, now i want to check whether $v value containes the value in "meta_value" second comma seperated field i'e second comma seperated value in meta_value field is "2", now i want to check whether $v containes 2 and i have one more variable $x=4, i want to check $x with third comma seperated field of meta_value field, can someone please help me on this?
Check out the documentation for the explode
function in php. You could get your meta_value column and explode it then do your compare i.e.
$meta = //get your meta_value field data here
$metaArray = explode(",", $meta);
$v == $metaArray[1]
$x == $metaArray[2]
I am assuming you got the "meta_value" value from database...
$val = $meta_value;
$newMeta = explode(",", $val);
$v = "2";
$x = "4";
for($i = 0; $i < count($newMeta); $i++){
if($newMeta[$i] == $v){
echo "found 2";
}elseif ($newMeta[$i] == $x) {
echo "found 4";
}else{
echo "not here";
}
//or do it like this:
//echo $newMeta[$i] == $v1 ? "found 2" : ($newMeta[$i] == $v2 ? "found 4" : "not here");
}
check out the PHP documentation for more details: http://www.php.net//manual/en/function.explode.php