0

I converted my csv to an associative array, but when I foreach that array I'm not getting the first element. I'm getting a warning like undefined index my code is as below

if ($ext1 == "csv" && $_FILES["fileb"]["error"] == 0) {
        $target1 = "upload/" . $_FILES["fileb"]["name"];
        move_uploaded_file($_FILES["fileb"]["tmp_name"], $target1);

        if (($handle1 = @fopen($target1, "r")) !== FALSE) {

            $array1 = $fields1 = array();
            $j = 0;

            while (($row1 = fgetcsv($handle1, 4096)) !== false) {
                if (empty($fields1)) {
                    $fields1 = $row1;

                    continue;
                }
                foreach ($row1 as $k => $value) {
                    $array1[$j][$fields1[$k]] = $value;
                }
                $j++;
            }

            fclose($handle1);
        }
    }

when foreach like

foreach($array1 as $arr)
{
   echo $arr['SCD']; // makes error like undefined index.
}
    but second element and all getting

when print_r , am getting like this

Array
(
    [0] => Array
        (
            [SCD] => s000172
            [SFN] => Alice
            [SSN] => Powell
            [SSC] => 09KG
            [PCD1] => 124405
            [PTI1] => Mrs
            [PFN1] => Penny
            [PSN1] => Powell
            [PEM1] => gppowell@tpg.com.au
            [CCD] => 09KG
            [CLS] => 09KG
            [CGN] => 09
            [TCD] => HENE01
            [TFN] => Erica
            [TSN] => Hennigar
            [TEM] => ehennigar@marymount.qld.edu.au
            [TCD2] => 
            [TFN2] => 
            [TSN2] => 
            [TEM2] => 
        )

    [1] => Array
        (
            [SCD] => s001272
            [SFN] => Jay
            [SSN] => Powell
            [SSC] => 11AB
            [PCD1] => 124405
            [PTI1] => Mrs
            [PFN1] => Penny
            [PSN1] => Powell
            [PEM1] => gppowell@tpg.com.au
            [CCD] => 11AB
            [CLS] => 11AB
            [CGN] => 11
            [TCD] => HORE02
            [TFN] => Ebony
            [TSN] => Horn
            [TEM] => Ebony.Horn@marymount.qld.edu.au
            [TCD2] => 
            [TFN2] => 
            [TSN2] => 
            [TEM2] => 
        )
user7441072
  • 285
  • 1
  • 4
  • 17

0 Answers0