2016-07-23 39 views
0

我有一個PHP腳本的作品,除了我得到這個錯誤信息PHP數組作爲索引,而不陣列

Undefined index: Array in [...]/exp.php on line 239 

在此行中有這樣的代碼很細:

$out_kostenstelle = $kostenstellen[$nextShift["kostenstelle"]][1]. 
    "(".$nextShift["kostenstelle"].")"; 

我覺得僅作爲索引的陣列可以發生的部分是$nextShift["kostenstelle"]$kostenstellen的索引的部分。

然而,當我試圖抓住這一部分(它與好幾百個運行一個循環,所以我不能手動檢查),此代碼,我的劇本從來沒有進入if條款內的部分

if(is_array($nextShift["kostenstelle"])) 
{ 
    echo "<pre>"; 
    var_dump($nextShift); 
    echo "</pre>"; 
    die(); 
} 

這對我沒有任何意義,我嘗試了很多東西。沒有成功。

我認爲這可能是足夠的代碼,其中的誤差可能,但萬一這裏是$kostenstellen結構和$nextShift

Kostenstellen:

array(2) { 
    [100]=> 
    array(2) { 
    [0]=> 
    string(3) "100" 
    [1]=> 
    string(11) "Company A" 
    } 
    [200]=> 
    array(2) { 
    [0]=> 
    string(3) "300" 
    [1]=> 
    string(12) "Company B" 
    } 
} 

和nextShift:

array(4) { 
    ["id"]=> 
    string(2) "168" 
    ["start_unix"]=> 
    string(10) "1466780000" 
    ["end_unix"]=> 
    string(10) "1466812400" 
    ["kostenstelle"]=> 
    string(3) "100" 
} 
+0

你把你的'is_array'檢查只是* *前的故障點? – BeetleJuice

+0

只是做了它,它什麼也沒有改變 – JRsz

回答

1

有沒有辦法繞過它:問題是你試圖使用的索引本身就是一個數組。

當您在php,$array[$index]中訪問數組時,如果PHP不是字符串或數字,它將嘗試對其進行字符串化。對數組進行字符串化將得到文字"Array";就像你在這裏一樣。

然而,還有另一種可能性:即當您運行循環,陣列已經字符串化。這意味着某個地方之前,有人將它鑄造成一個字符串。

你可以與如果檢查這樣的,如果:

if(is_array($nextShift["kostenstelle"]) || $nextShift["kostenstelle"] == "Array") 
{ 
    echo "<pre>"; 
    var_dump($nextShift); 
    echo "</pre>"; 
    die(); 
} 
+0

僅供參考,當'array'與任何東西比較時,'array'總是更大。 –

+0

好吧,我看到了,所以我把數組放在了我的日期的某個點,然後它沒有觸發,因爲字符串是數組,因爲前面的數組。我誤解了錯誤。這工作,現在我可以修復損壞的數據。謝謝你,先生 :) – JRsz