2017-07-19 33 views
2

我試圖訪問數組的數組鍵:最佳實踐檢查多個array_key_exists在PHP

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

的方式我做它的偉大工程,如果反應出來很好如我所料。 如果沒有,我會得到這樣的事情:

未定義指數:SAMLP:響應

我已經試過:

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.'); 
} 

什麼是正確的方法或最佳做法來檢查這樣的事情?人們會如何去做並做到這一點?

回答

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; 

你也可以檢查出Getter功能How to write getter/setter to access multi-level array by key names?並通過這樣的事情:

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

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