在PHP中检查多个array_key_exists的最佳实践

时间:2017-07-19 20:01:04

标签: php arrays multidimensional-array

我正在尝试访问数组的数组键:

$attributes = $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE']['SAML:ATTRIBUTEVALUE'];

如果响应按照我的预期很好地完成,那么我的方式效果很好。 如果没有,我会得到这样的东西:

  

未定义的索引:SAMLP:RESPONSE

我试过了:

try {
  $attributes = $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE']['SAML:ATTRIBUTEVALUE'];
} catch (Exception $e) {
    Helper::console("Bad SAML RESPONSE.");
    dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}

我试图避免进行大量的array_key_exists检查:

if (array_key_exists('SAMLP:RESPONSE', $xml)) {
    if (array_key_exists('SAML:ASSERTION', $xml['SAMLP:RESPONSE'])) {
        if (array_key_exists('SAML:ATTRIBUTESTATEMENT', $xml['SAMLP:RESPONSE']['SAML:ASSERTION'])) {

            if (array_key_exists('SAML:ATTRIBUTE', $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT'])) {

                if (array_key_exists('SAML:ATTRIBUTEVALUE', $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE'])) {

                    $attributes = $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE']['SAML:ATTRIBUTEVALUE'];

                }else{
                    Helper::console("['SAML:ATTRIBUTEVALUE'] key not exist");
                    dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
                }

            }else{
                Helper::console("['SAML:ATTRIBUTE'] key not exist");
                dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
            }
        }else{
            Helper::console("['SAML:ATTRIBUTESTATEMENT'] key not exist");
            dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
        }
    }else{

        Helper::console("['SAMLP:RESPONSE'] key not exist");
        dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
    }

} else {
    Helper::console('SAMLP:RESPONSE key not exist');
    dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}

检查此类内容的正确方法或最佳做法是什么?怎么会去做呢?

1 个答案:

答案 0 :(得分:2)

虽然很长,但isset会有效:

if(isset($xml['SAMLP:RESPONSE']
             ['SAML:ASSERTION']
             ['SAML:ATTRIBUTESTATEMENT']
             ['SAML:ATTRIBUTE']
             ['SAML:ATTRIBUTEVALUE'])) {

    $attributes = $xml['SAMLP:RESPONSE']
                      ['SAML:ASSERTION']
                      ['SAML:ATTRIBUTESTATEMENT']
                      ['SAML:ATTRIBUTE']
                      ['SAML:ATTRIBUTEVALUE'];
}

或者在PHP 7中Null coalescing operator将分配,如果设置或分配备用值,如果不是:

$attributes = $xml['SAMLP:RESPONSE']
                  ['SAML:ASSERTION']
                  ['SAML:ATTRIBUTESTATEMENT']
                  ['SAML:ATTRIBUTE']
                  ['SAML:ATTRIBUTEVALUE'] ?? null;

您还可以查看How to write getter/setter to access multi-level array by key names?中的 Getter 功能并将其传递给您:

$path = "SAMLP:RESPONSE.SAML:ASSERTION.SAML:ATTRIBUTESTATEMENT.SAML:ATTRIBUTE.SAML:ATTRIBUTEVALUE";
$attributes = get($path, $xml); //returns NULL if the path doesn't exist

我使用了.分隔符,但您可以使用:以外的任何分隔符,例如/-