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.');
}
什麼是正確的方法或最佳做法來檢查這樣的事情?人們會如何去做並做到這一點?