0

I am trying to validate 3 scenarios but something is wrong and the if statements are not being honored properly

Here are the scenarios I need to work:

  1. If the string "2.5.4.15" exists in array then output "Extended Validation".
  2. If the string "2.5.4.15" does NOT exist in array then output "Organization Validated".
  3. If the string "id-at-organizationName" does NOT exist in array then output "Domain Validated".

I am getting incorrect results. For example, if the data I am parsing does contains "2.5.4.15" for some reason its returning "Domain Validated" ??

Here is my code:

    if(isset($cert['tbsCertificate']['subject']['rdnSequence'])) {
        $EV = array('2.5.4.15');
        $org = array('id-at-organizationName');
        $count = count($cert['tbsCertificate']['subject']['rdnSequence']);
        for($i = 0; $i < $count; $i++) {
            if(in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $EV)) {
                $validation = "<tr><td>Validation</td><td>Extended Validation (EV)</td></tr>";
                echo $validation;
                break;
            }
            if(!in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $EV)) {
                $validation = "<tr><td>Validation</td><td>Organization Validated (OV)</td></tr>";
                echo $validation;
                break;
            }
            if(!in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $org)) {
                $validation = "<tr><td>Validation</td><td>Domain Validated (DV)</td></tr>";
                echo $validation;
                break;
            }
        }
    }

-- UPDATE --

I removed the break; and I can see it's now returning multiple results before it gives our the correct one (highlighted in red the correct match).. but why is it returning the bogus response instead of just returning the correct response the first time ?

enter image description here

-- UPDATE 2 --

I think I understand the results I am getting, it seems to be outputting result for each iteration where the string is not found. Whereas what I want to do is return one response.

I think then because of this perhaps using a loop is not the answer. Is there a way to search the whole array for a string and output the result instead of looping through each array ?

6
  • Are you willing to check if the string exists as a value in the array or as a part of the value? Commented May 11, 2015 at 5:24
  • it was a question not a suggestion ;) Regarding to your recent updates - can you please provide an example (of input) with the desired output? It's hard to understand what are you exactly trying to achieve. Commented May 11, 2015 at 7:22
  • I updated my post with more info as I now understand why i am getting the results .. because I am using !in_array is was giving me an output each time no match was found.. how can I search the entire array for a string instead ? Commented May 11, 2015 at 7:25
  • An example of input besides output would be the best. Loop should be the answer but you can maybe try using array_map (php.net/manual/en/function.array-map.php) ? Commented May 11, 2015 at 7:27
  • Here is the input pastebin.com/KKbnNUzj - i've highlighted in yellow the array in question. I am basically wanting to search in this path ['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'] Commented May 11, 2015 at 7:42

2 Answers 2

1
  • I didn't understand why are you using 'array' to store single values, you can simple compare strings.
  • In case the field can match only to one option - you can use if..elseif or even better - switch.
  • Please notice that your $validation variable will always overwrite itself in every irritation. So, If you're looking for a specific row - you should mention it. If you're looking for one multi-result in the end, you need to store that data in another array.

In continuation to the chat, let me break the scenarios of the key's value:

  1. If 2.5.4.15 exists in the array - return EV
  2. If it (1) doesn't exist but 'id-at-organizationName' does - return
  3. If it (1) doesn't exist and (2) also doesn't exist - return

For the first scenario I used break since if it exists we don't need to continue to check the rest of the array, also it's automatically means that the 2 other conditions could never exist. EDIT also added a break to the second scenario condition.

Here is my suggestion: (Please check it and share the output/problems in the chat until will accomplish to solve your problem)

       if(isset($cert['tbsCertificate']['subject']['rdnSequence'])) {
            $count = count($cert['tbsCertificate']['subject']['rdnSequence']);
            for($i = 0; $i < $count; $i++) {
               $value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'];
               if($value == "2.5.4.15") {
                  $output = "EV";
                  break;
               } else {
                  if($value == "id-at-organizationName") {
                    $output = "OV";
                    break; //Edit 1
                  } else {
                    $output = "DV";
                  }
               }
            }
echo $output;
       }
Sign up to request clarification or add additional context in comments.

1 Comment

Ofir, thanks for your professionalism and time taken to understand my requirements thoroughly. You have resolved my problem swiftly and the solution is very well received. thanks very much!
0

You echoes bad variables:

         for($i = 0; $i < $count; $i++) {
            if(in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $EV)) {
                $validation = "<tr><td>Validation</td><td>Extended Validation (EV)</td></tr>";
                echo $validation;
                break;
            }
            if(!in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $EV)) {
                $validation1 = "<tr><td>Validation</td><td>Organization Validated (OV)</td></tr>";
                echo $validation1;  // $validation1 instead of $validation
                break;
            }
            if(!in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $org)) {
                $validation2 = "<tr><td>Validation</td><td>Domain Validated (DV)</td></tr>";
                echo $validation2; // $validation2 instead of $validation
                break;
            }
        }

The second thing is that for this task is better to use elseif instead of 3 ifs:

for () {
    if () {
        // validation
    } elseif () {
        // validation 1
    } elseif () {
        // validation 2
    }
}

2 Comments

yeah, sorry, that was a typo when cleaning up my code when posting. i updated my post. it was not the issue. I still have the problem.
i think there might be something wrong with the loop? i updated my post with more details and a screenshot of the output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.