检查数组是否包含$ _GET ['id']

时间:2013-02-10 16:53:56

标签: php mysql arrays get

所以我在查询数据库,它会返回一个id的数组。

$val = $db->prepare("SELECT x FROM xx WHERE x_id='$xx'");
$val->execute();
$res = $val->fetchAll(PDO::FETCH_COLUMN, 0);

然后,我将用逗号分隔数组,

$x= join('', $res);

并回显它将返回1,18,32

然后我想检查$_GET['id']是否是这些数字之一

if($_GET['id] contains one of these){ // so here lies the problem, how do I check that it contains 1/18 or 32?
//continue...
}
else{
echo "You have no right to view this.";
}

但是怎么样?

1 个答案:

答案 0 :(得分:2)

使用in_array(val, array)

if(in_array($_GET['id'], $res)) echo "Welcome";
else echo "You have no business here.";

参考:http://php.net/manual/en/function.in-array.php