PHP-检查多维数组中的键是否为真
我试图根据多维数组中的三个布尔值设置变量的值,但访问它时遇到了问题。我对PHP和Javascript有点陌生 在对我的表单数据进行var_转储(使用$http从angular传递到外部PHP文件)之后,剩下的是:PHP-检查多维数组中的键是否为真,php,angularjs,webforms,Php,Angularjs,Webforms,我试图根据多维数组中的三个布尔值设置变量的值,但访问它时遇到了问题。我对PHP和Javascript有点陌生 在对我的表单数据进行var_转储(使用$http从angular传递到外部PHP文件)之后,剩下的是: array(3) { ["selectedServices"]=> array(3) { ["test"]=> bool(true) ["test2"]=> bool(false) ["test3"]=>
array(3) {
["selectedServices"]=>
array(3) {
["test"]=>
bool(true)
["test2"]=>
bool(false)
["test3"]=>
bool(false)
}
["otherstuff"]=>
string(10) "coolfield"
["smsDelRep"]=>
bool(false)
}
现在我正试图创建一个基于“selectedServices”的变量,但到目前为止所有的尝试都失败了,我甚至无法访问它
目前我拥有的是:
$_POST = json_decode(file_get_contents('php://input'), true);
$selectedServices = array($_POST['selectedServices']);
if (in_array('test', $selectedServices, true)) {
$allowedServices = '1';
var_dump($allowedServices);
但是我没有得到var_转储,所以我猜if语句返回false。但是为什么呢?在数组中检查值,而不是键,您可以执行以下操作:
if(isset($selectedServices['test']) {
//
}
编辑:这不是一个完美的解决方案,如果$selectedServices['test']为空,它将不起作用
改用数组\键\存在:
if(array_key_exists('test', $selectedServices)) {
//
}
但您的代码中有一个错误:
$selectedServices = array($_POST['selectedServices']);
应该是
$selectedServices = $_POST['selectedServices'];
在数组中检查值,而不是键,您可以执行以下操作:
if(isset($selectedServices['test']) {
//
}
编辑:这不是一个完美的解决方案,如果$selectedServices['test']为空,它将不起作用
改用数组\键\存在:
if(array_key_exists('test', $selectedServices)) {
//
}
但您的代码中有一个错误:
$selectedServices = array($_POST['selectedServices']);
应该是
$selectedServices = $_POST['selectedServices'];
您希望检查数组键而不是将键作为值搜索。为什么要将帖子包装在数组中?您希望检查数组键而不是将键作为值搜索。为什么要将帖子包装在数组中?Hrm,它仍然会变为false;如果(isset($selectedServices['test']){echo“yay”;else{echo“nay”;}给我“nay”:(Hrm,它仍然变为false;如果(isset($selectedServices['test']){echo“yay”;else{echo“nay”;}给我“nay”:(