PHP logic problem

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 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.

    
asked by anonymous 04.04.2017 / 10:09

2 answers

3

You can simply use && (empty($modality) || $plans_available[$key]['modality'] == $modality) .

$found = 0;

$get_arr_key = array_search($idplan, array_column($plans_available, 'plan'));

if($get_arr_key !== false
    && in_array($method, $plans_available[$get_arr_key]['allowed_methods'])
    && (empty($modality) || $plans_available[$get_arr_key]['modality'] == $modality)){

    $found = 1;

}

var_dump($found);
  

/! \ O empty() considers that 0 is empty !

Try this here.

If you want to create functions with slightly more meaningful names, just an example:

$plano_selecionado = getArrayKeyPlano($plans_available, $idplan);

$found = $plano_selecionado !== false
         && isMethodDisponivel($plans_available, $plano_selecionado, $method)
         && isModalityDisponivel($plans_available, $plano_selecionado, $modality);

function getArrayKeyPlano($array, $idPlano){
    return array_search($idPlano, array_column($array, 'plan'));
}

function isMethodDisponivel($array, $array_key, $metodo){
    return in_array($metodo, $array[$array_key]['allowed_methods']);
}

function isModalityDisponivel($array, $array_key, $modalidade){
    return (empty($modalidade) || $array[$array_key]['modality'] == $modalidade);
}

Next to "as is":

for($i = 0; $i < count($plans_available); $i++){
    if ( $plans_available[$i]["plan"] == $idplan
        && in_array($method, $plans_available[$i]["allowed_methods"])
        && (empty($modality) || $plans_available[$i]['modality'] == $modality) ){

        $found = 1;

    }
}

Try this.

    
04.04.2017 / 13:04
4

Use array_filter

With this function you can specify a filter by plane and method and if you do not return any element of the array it is because it does not exist. If you need to know the array items that satisfy the condition, just pick up the result of the call.

$found = !empty(array_filter($plans_available, function ($i) use ($idplan, $method)  { 
    return key_exists("plan",$i) && 
           key_exists("allowed_methods", $i) && 
           $i["plan"] == $idplan && 
           in_array($method, $i["allowed_methods"]); 
}));
    
04.04.2017 / 13:57