mercredi 12 juillet 2017

How can I simply check various variables for one statement?

I want to check about 7 variables for the same statement, but it is not required. All that variables have an array with custom keys.

I have, for example, this code:

$generalSettings = array(
"lights" = $_POST['generalLights'],
"tones" = $_POST['generalTones']
);

(And several more.)

Now, I want to check if the value is 1; if not, the value has to be set to 0.

I would do it like this:

if($generalSettings['ligts'] == 1) {
$generalSettings['lights'] = 1;
} else {
$generalSettings['lights'] = 0;
}
if($generalSettings['tones'] = 1) {
$generalSettings['tones'] = 1;
} else {
$generalSettings['tones'] = 0;
}

This is a lot of work, if you think of 3 arrays with 4 items and four arrays with five items.

My question now is: Can I do this in a simpler way, and if yes, how?

Aucun commentaire:

Enregistrer un commentaire