簡體   English   中英

查找多級數組中的哪個索引會在PHP中引發未定義的索引錯誤

[英]Find which index in a multi level array throws undefined index error in PHP

所以我有一個嵌套的php數組,我正在使用它來獲取數據:

$this->synthArray[$synthId]['synth_map'][$mapId]['map_sequence'][$gnome];

這行有時會給我錯誤undefined index錯誤。 我正在嘗試找出哪個索引產生了該錯誤-我可以在每個級別上執行isset()來實現此目的,但是我想知道是否有更簡單的方法來找到罪魁禍首...

編輯您的數組有3個故障點...我的意思是數組中可以缺少3個變量。

$this->synthArray[$synthId]['synth_map'][$mapId]['map_sequence'][$gnome];

您可以將其檢查為:

if(isset($this->synthArray[$synthId])) {
     if(isset($this->synthArray[$synthId]['synth_map'][$mapId])) {
         if(isset($this->synthArray[$synthId]['synth_map'][$mapId]['map_sequence'][$gnome])) {
             // it's correct
         } else {
             //$gnome is an invalid key
         }
     } else {
          // $mapId is an invalid key
     }
} else {
    // $synthId is an invalid key
}

您可以使用set_error_handler注冊您自己的錯誤處理程序。

function errorHandler( $errno, $errstr, $errfile, $errline ) {
    // catch the error here and take action
    echo "{$errstr} in file {$errfile} on line {$errline}"; // example

    /* Don't execute PHP internal error handler */
    return true;
}
set_error_handler('errorHandler');

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM