phparraysmultidimensional-arraymerging-data

Populate a 2d arrays with iterated results containing 2d arrays


I'm trying to build an array then traverse it but keep getting undefined index errors. What am I doing wrong here? Is the array not built correctly? If I don't push new items into it, the code works fine. As soon as I add more elements into it, the whole thing breaks.

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

When I try to access row data, I get undefined index errors.

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'];
    // omitted code where I use the temporary variables
}

$pickUpData:

    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)

Solution

  • It looks like the array has an extra level that's being overlooked... Maybe try something like this:

    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'];
      }
    }