数组函数中的php
我在数组()中使用数组函数中的php,php,arrays,Php,Arrays,我在数组()中使用,检查数组中是否存在值,如下所示, 但没有如预期的那样起作用: $projectId = $_GET['projectId']; $userId = $_GET['userId']; $qr2 = mysql_query("SELECT `userId`,`status` FROM `fndn_ProjectBacker` WHERE `projectId`='$projectId' && `status`='1'"); for($i = 1; $row2
,
检查数组中是否存在值,如下所示,
但没有如预期的那样起作用:
$projectId = $_GET['projectId'];
$userId = $_GET['userId'];
$qr2 = mysql_query("SELECT `userId`,`status` FROM `fndn_ProjectBacker` WHERE `projectId`='$projectId' && `status`='1'");
for($i = 1; $row2 = mysql_fetch_assoc($qr2); $i++) {
$arr1[$i] = array($row2);
}
if (in_array($userId, $arr1)) {
echo "True";
}
请帮帮我 它不能按您的要求工作,因为您使用以下内容创建二维数组:
$arr1[$i]=array($row2);
//^^^^^^ ^ See here
删除此处的数组声明,还可以使用while循环更改for循环,例如
while($row2 = mysql_fetch_assoc($qr2)){
$arr1[] = $row2["userId"];
}
if (in_array($userId, $arr1)) {
echo "True";
}
我还想欢迎您在2015年光临,并强烈建议您使用或,它们更安全。(另见:)@fatemehnamkhaha什么是不工作?你有错误吗?将错误报告添加到文件顶部:
以及输出内容:打印($arr1)代码>在while循环之后?数组([0]=>Array([userId]=>0[status]=>1)[1]=>Array([userId]=>5[status]=>1)[2]=>Array([userId]=>25[status]=>1).@fatemehnamkha更新了我的答案。只需重新加载页面并重试代码。您只需要将userId添加到数组中。