1
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 would verify the following:
If the modality is reported, it should be tested to match the reported plan, but only test the plan and permitted methods.
However I caught on to that check, in the above code 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 doesn’t match what I need.
It doesn’t get any simpler
$plans = array( '1' => array( "allowed...
where the key is the plan id? pq ai vc will simply testin_array( $method, $plans[$plano_informado]['allowed_methods'] )
– Bacco
The
$modality
comes from the POST also?– Inkeliz
@Bacco I just gave an example, because actually the id of the plan is another :P
– MarceloBoni
I edited with an example of the array that more or less matches my reality
– MarceloBoni