在现有PHP脚本中添加if else语句

时间:2015-04-20 04:39:18

标签: php

希望对你们来说很容易。我有一个现有的循环,我想添加一个else语句,但当我尝试,它的工作,但如果我在else语句中做一个回声我得到它的多次迭代所以我怀疑else语句不在正确的地方或其他东西需要发生。

else { echo "no results found"; }

基本上我希望它检查是否存在: array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value']) && in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $oid)

如果没有,则返回else语句else { echo "no results found"; }

$count = count($cert['tbsCertificate']['subject']['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString');
$oid = array('2.5.4.15', '1.3.6.1.4.1.311.60.2.1.3', '1.3.6.1.4.1.311.60.2.1.2', 'id-at-streetAddress');
for($i = 0; $i < $count; $i++) {
    foreach($exists as $field) {
        if(
            array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value']) &&
            in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $oid)
        ) {
            $value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'][$field];
            $type = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'];
            echo '     ', str_replace(array_keys($replacements), $replacements, $field), ' : ', str_replace(array_keys($replacements), $replacements, $type), '   ', $value,"\n";
            break; // stop further looping
        } else { echo "no results found"; }
    }
}

使用@ GRC的解决方案,结果如下:

enter image description here

3 个答案:

答案 0 :(得分:2)

如果找到值 ,您可以使用continue 2;跳出foreach并继续下一次for次迭代。然后,将你的其他身体放在foreach

下面
for($i = 0; $i < $count; $i++) {
    foreach($exists as $field) {
        if(
            array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value']) &&
            in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $oid)
        ) {
            $value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'][$field];
            $type = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'];
            echo '     ', str_replace(array_keys($replacements), $replacements, $field), ' : ', str_replace(array_keys($replacements), $replacements, $type), '   ', $value,"\n";

            continue 2; // stop looping and continue with for loop
        }
    }
    // nothing found in the whole foreach
    echo "no results found for $i\n";
    break; // jump out
}

答案 1 :(得分:1)

试试这个

     $count = count($cert['tbsCertificate']['subject']['rdnSequence']);
        $exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString');
        $oid = array('2.5.4.15', '1.3.6.1.4.1.311.60.2.1.3', '1.3.6.1.4.1.311.60.2.1.2', 'id-at-streetAddress');
    $else = false;
        for($i = 0; $i < $count; $i++) {
            foreach($exists as $field) {
                if(
                    array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value']) &&
                    in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $oid)
                ) {
                    $value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'][$field];
                    $type = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'];
                    echo '     ', str_replace(array_keys($replacements), $replacements, $field), ' : ', str_replace(array_keys($replacements), $replacements, $type), '   ', $value,"\n";
                    break; // stop further looping
                } else { 
                    echo "no results found"; 
                      $else = true;
                        break; 
                    }
            }
          if($else)
              break;
        }

答案 2 :(得分:1)

你可以这样做

$count = count($cert['tbsCertificate']['subject']['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString');
$oid = array('2.5.4.15', '1.3.6.1.4.1.311.60.2.1.3', '1.3.6.1.4.1.311.60.2.1.2', 'id-at-streetAddress');

for($i = 0; $i < $count; $i++) {
    foreach($exists as $field) {
        if(
            array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value']) &&
            in_array($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'], $oid)
        ) {
            $value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'][$field];
            $type = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type'];
            echo '     ', str_replace(array_keys($replacements), $replacements, $field), ' : ', str_replace(array_keys($replacements), $replacements, $type), '   ', $value,"\n";
            break; // stop further looping
        } else {
           if($i == 0) {
             echo "No more data";
           }
           break;
        }
    }
}