我正在使用亞馬遜產品API。多維數組中的未定義索引
$ItemAttr = $Item['ItemAttributes'];
現在$ItemAttr
包含一個多維數組。
if(is_array($ItemAttr["Author"])){$Author = implode(", ", $ItemAttr["Author"]);
}else{
$Author = $ItemAttr["Author"];}
現在,當我使用上面的代碼我得到Undefined index: Author in line 1 and line 3
我試過這樣
if(isset($ItemAttr["Author"])) {
if(is_array($ItemAttr["Author"])){$Author = implode(", ", $ItemAttr["Author"]);
}else{
$Author = $ItemAttr["Author"];}
}
它消除了錯誤。
可是後來,當我使用這樣$RetVal = array('Author' => $Author);
代碼我得到Undefined variable : Author
錯誤
誰能告訴我正確的方法是什麼?
請注意:$Item['ItemAttributes'];
可能或可能不包含Author
鍵。我的意思是如果返回的產品是一本書,數組將返回作者密鑰。要不然也不會..
上次編輯使我的評論過時;)。 –
這是有道理的。感謝你們兩位:) – user1091558
這將消除錯誤,但你仍然更好地創建一個新的結構來處理,並有你需要的保證。寫出你想要訪問的東西時,就是偏執編碼,這是最糟糕的。我在上個月使用亞馬遜API進行圖書搜索時的回答中解釋了更多 - 現在我在給予機會時避開了亞馬遜API! – VBAssassin