如何检查两个不同数组中的字符串是否匹配

时间:2022-01-19 12:12:48

I would like to check if a string $value in two different arrays match. If they match, do something. If they don't match, do something.

我想检查两个不同数组中的字符串$值是否匹配。如果他们匹配,做一些事情。如果他们不匹配,做一些事情。

Here is the code to get the string value seperately but I would like if possible to combine them together and then compare the strings for a match.

下面是单独获取字符串值的代码,但我希望尽可能将它们组合在一起然后比较字符串以进行匹配。

Here is the first lookup

这是第一次查找

        $count = count($cert['tbsCertificate']['subject']['rdnSequence']);
        $exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
        $oid = array('id-at-commonName');
        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];
                    echo $value;
                    break; // stop further looping
                }
            }
        }

Here is the second lookup:

这是第二次查找:

        $count = count($cert['tbsCertificate']['issuer']['rdnSequence']);
        $exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
        $oid = array('id-at-commonName');
        for($i = 0; $i < $count; $i++) {
            foreach($exists as $field) {
                if(
                    array_key_exists($field, $cert['tbsCertificate']['issuer']['rdnSequence'][$i][0]['value']) &&
                    in_array($cert['tbsCertificate']['issuer']['rdnSequence'][$i][0]['type'], $oid)
                ) {
                    $value = $cert['tbsCertificate']['issuer']['rdnSequence'][$i][0]['value'][$field];
                    echo $value;
                    break; // stop further looping
                }
            }
        }

1 个解决方案

#1


Put your lookup code in a function that takes the type as a parameter, and returns the value found. Then compare the two calls.

将查找代码放在以类型作为参数的函数中,并返回找到的值。然后比较两个电话。

function lookup($cert, $type) {
    $count = count($cert['tbsCertificate'][$type]['rdnSequence']);
    $exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
    $oid = array('id-at-commonName');
    for($i = 0; $i < $count; $i++) {
        foreach($exists as $field) {
            if(
                array_key_exists($field, $cert['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
                in_array($cert['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
            ) {
                $value = $cert['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
                return $value;
            }
        }
    }
    return null;
}

if (lookup($cert, 'subject') == lookup($cert, 'issuer')) {
    // do something
}

#1


Put your lookup code in a function that takes the type as a parameter, and returns the value found. Then compare the two calls.

将查找代码放在以类型作为参数的函数中,并返回找到的值。然后比较两个电话。

function lookup($cert, $type) {
    $count = count($cert['tbsCertificate'][$type]['rdnSequence']);
    $exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
    $oid = array('id-at-commonName');
    for($i = 0; $i < $count; $i++) {
        foreach($exists as $field) {
            if(
                array_key_exists($field, $cert['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
                in_array($cert['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
            ) {
                $value = $cert['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
                return $value;
            }
        }
    }
    return null;
}

if (lookup($cert, 'subject') == lookup($cert, 'issuer')) {
    // do something
}