I have the following structure in PHP:
<?php
header('Content-Type: application/json; charset=utf-8');
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_STRICT);
ini_set('display_errors', 'On');
$idplan = ( empty($_POST['idplano']) ? "" : $_POST['idplano'] );
$method = ( empty($_POST['metodo']) ? "" : $_POST['metodo'] );
if ( empty($idplan) || empty($method) ) {
http_response_code(401);
die('{"msg": "Plano ou Método não informado."}');
}
$plans_available = array(
array(
"plan" => 22,
"allowed_methods" => array(
"creditcard",
"giftcard"
),
"modality" => "mensal"
),
array(
"plan" => 23,
"allowed_methods" => array(
"creditcard"
),
"modality" => "mensal"
),
array(
"plan" => 30,
"allowed_methods" => array(
"boleto"
),
"modality" => "trimestral"
),
array(
"plan" => 31,
"allowed_methods" => array(
"boleto"
),
"modality" => "semestral"
),
array(
"plan" => 32,
"allowed_methods" => array(
"boleto"
),
"modality" => "anual"
),
array(
"plan" => 33,
"allowed_methods" => array(
"boleto"
),
"modality" => "trimestral"
),
array(
"plan" => 34,
"allowed_methods" => array(
"boleto"
),
"modality" => "semestral"
),
array(
"plan" => 35,
"allowed_methods" => array(
"boleto"
),
"modality" => "anual"
)
);
$found = 0;
for($i = 0; $i < count($plans_available); $i++){
// Teste condicional
if ($plans_available[$i]["plan"] == $idplan && in_array($method, $plans_available[$i]["allowed_methods"])){
$found = 1;
}
}
if(!$found){
http_response_code(401);
die('{"msg": "Plano não encontrado."}');
}
die("encontrado!");
?>
I tried to create a logic that verified the following:
If the modality is informed, one must test whether it matches the given plan, or else test only the plan and the methods allowed.
But I checked this check, in the code above I do not check the mode, and it works!
I tried something like:
if ( $plans_available[$i]["plan"] == $idplan && in_array($method, $plans_available[$i]["allowed_methods"]) && empty($modality) ){
$found = 1;
} else if ( $plans_available[$i]["plan"] == $idplan && in_array($method, $plans_available[$i]["allowed_methods"]) && $plans_available[$i]["modality"] == $modality ){
$found = 1;
}
It just does not match what I need.