I'm trying to set the value of a variable based on three booleans from a multidimensional array but I'm having trouble accessing it. I'm kinda new to PHP and Javascript.
After a var_dump of my form data (passed using $http from angular to an external PHP-file) I'm left with:
array(3) {
["selectedServices"]=>
array(3) {
["test"]=>
bool(true)
["test2"]=>
bool(false)
["test3"]=>
bool(false)
}
["otherstuff"]=>
string(10) "coolfield"
["smsDelRep"]=>
bool(false)
}
Now I'm trying to create a variable based on "selectedServices" but all attempts so far has failed, I can't even access it.
Currently this is what I have:
$_POST = json_decode(file_get_contents('php://input'), true);
$selectedServices = array($_POST['selectedServices']);
if (in_array('test', $selectedServices, true)) {
$allowedServices = '1';
var_dump($allowedServices);
But I'm not getting the var_dump so I'm guessing the if-statement returns false. But why?
In_array checks values, not keys, you could do :
if(isset($selectedServices['test']) {
//
}
EDIT : This is not a perfect solution, if $selectedServices['test'] is null it will not work
Use array_key_exist instead :
if(array_key_exists('test', $selectedServices)) {
//
}
But there is an error in your code :
$selectedServices = array($_POST['selectedServices']);
should be
$selectedServices = $_POST['selectedServices'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.