2014-06-13 115 views
1

我想構建一個數組然後遍歷它,但不斷收到未定義的索引錯誤。我在這裏做錯了什麼?該數組未正確構建?如果我不推送新項目,代碼工作正常。一旦我添加更多元素,整個事情就會崩潰。PHP推動陣列問題

foreach($CustPOIDs as $key => $val){ 
        $CustPOID = $val['CustPOID']; 
         //Call this method for each CustPOID 
        $tender = new BOL($CustPOID); 
        $pickUpData[]=$tender->getPickupInfo(); 
       } 
      } 

      foreach ($pickUpData as $key => $val) { 
       $LoadDate = $val['POLineLoadDate']; 
       $POLineComments = $val['POLineComment']; 
       $ProdID = $val['ProdID']; 
       $ShipperId = $val['ShipperId']; 
       $ShipDesc = $val['Description']; 
       $shAddress = $val['Address1']; 
       $shState = $val['State']; 
       $shPhone = $val['Phone']; 
       $pdProdDesc = $val['ProdDesc']; 
       $pdCommodity = $val['Commodity']; 
       $pdWeight = $val['ProdWeight']; 
      } 

array (size=2) 
    0 => 
    array (size=1) 
     0 => 
     array (size=12) 
      'POLineLoadDate' => string '1969-12-31 00:00:00' (length=19) 
      'POLineComment' => string '56sent NOT 60' (length=13) 
      'ProdID' => string '322' (length=3) 
      'ShipperId' => null 
      'Description' => null 
      'Address1' => null 
      'City' => null 
      'State' => null 
      'Phone' => null 
      'ProdDesc' => string 'SLESS 60CT Bin/Bin WMELON US#1' (length=30) 
      'Commodity' => string 'WATERMELON' (length=10) 
      'ProdWeight' => string '0' (length=1) 
    1 => 
    array (size=2) 
     0 => 
     array (size=12) 
      'POLineLoadDate' => string '1900-01-01 00:00:00' (length=19) 
      'POLineComment' => string '' (length=0) 
      'ProdID' => string '192' (length=3) 
      'ShipperId' => null 
      'Description' => null 
      'Address1' => null 
      'City' => null 
      'State' => null 
      'Phone' => null 
      'ProdDesc' => string 'RND WHT US#1 SIZE A PAPR POTAT' (length=30) 
      'Commodity' => string 'POTATO' (length=6) 
      'ProdWeight' => string '0' (length=1) 
     1 => 
     array (size=12) 
      'POLineLoadDate' => string '1900-01-01 00:00:00' (length=19) 
      'POLineComment' => string '' (length=0) 
      'ProdID' => string '187' (length=3) 
      'ShipperId' => null 
      'Description' => null 
      'Address1' => null 
      'City' => null 
      'State' => null 
      'Phone' => null 
      'ProdDesc' => string 'IDAHO US#1 6-10OZ POLY POTATO' (length=29) 
      'Commodity' => string 'POTATO' (length=6) 
      'ProdWeight' => string '0' (length=1) 
+0

「*,但不斷收到未定義的索引錯誤* 「 - 請發佈確切的錯誤消息。 –

+0

在第一個foreach中你已經有了一個額外的花括號 - 可能只是其餘的代碼剩餘的,但我認爲我會指出它 –

+0

額外的花括號是針對它上面的條件。通知:未定義指數:POLineLoadDate在C:\瓦帕\ WWW \ XXXXXXX \ takeIII \ lEditor.php上線245 調用棧 #\t \t時間內存\t \t功能位置 \t 0.0010 211552 \t {主}()\t .. \ lEditor.php:0 – Waragi

回答

1

它看起來像陣列具有真實被忽視的額外水平...也許嘗試這樣的事情:

for ($i = 0; $i < count($pickUpData); $i++) { 
    foreach ($pickUpData[$i] as $key => $val) { 
    $LoadDate = $val['POLineLoadDate']; 
    $POLineComments = $val['POLineComment']; 
    $ProdID = $val['ProdID']; 
    $ShipperId = $val['ShipperId']; 
    $ShipDesc = $val['Description']; 
    $shAddress = $val['Address1']; 
    $shState = $val['State']; 
    $shPhone = $val['Phone']; 
    $pdProdDesc = $val['ProdDesc']; 
    $pdCommodity = $val['Commodity']; 
    $pdWeight = $val['ProdWeight']; 
    } 
} 
+0

謝謝@jerdiggity – Waragi

+0

這工作得很好,但我的問題仍然存在......爲什麼當我建立一個數組爲什麼有一個額外的水平?我是否錯誤地構建數組? – Waragi

+1

@Waragi不,我不認爲你在「錯誤地」構建它們......可能發生的事情是'$ tender-> getPickupInfo()'也可以作爲一個數組返回。事實上,你可以使用你原來的代碼實現相同的結果,但用下面的代碼替換'$ pickUpData [] = $ tender-> getPickupInfo();''pickUpInfo [$ key] = $ tender-> getPickupInfo() ; $ pickUpData [] = $ pickUpInfo [$鍵] [0];' – jerdiggity