使用php从csv读取

时间:2018-03-17 03:30:12

标签: php arrays csv multidimensional-array rows

我无法打印csv的所有行。我能够阅读第一个。我的csv有3行。我怎样才能获得所有行?被困在这很长一段时间了。请帮忙!谢谢!这是我的代码

if (file_exists($fname)) {
    if (($handle1 = fopen($fname, "r")) !== FALSE) {
        while ( ($data1= fgetcsv($handle1, 1000, ",")) !== FALSE) {
            $num1 = count($data1);
            $row++; 

            //  echo "fileid=$fileid;  owner=$data1[6],$owner; telephone=$data1[5],$telephone; id=$data1[0],$id<br>";
            $cond = 0;
            if ($id != '')
                {if( $data1[0]  === $id ) $cond= 1;}
            else if ($owner != '')
            {  
                if ($data1[6]  === $owner ) $cond = 1;
            }
            else if($telephone!='')
            {
                if ($data1[5] === $telephone) 
                $cond=1;
            }

            if($cond==1)
            {  
                print($num1);
                for ($c=0; $c < $num1; $c++) {
                    $a[$c] =  $data1[$c] ;
                }

              break 2;
            }
        }
        fclose($handle1);
    }
}

// Array ( [0] => 54 [1] => xyz [2] => Sara [3] => saarah@gmail.com [4] => 16/03/18 [5] => [6] => Sara [7] => R T Nagar [8] => BOVINE [9] => Hallikar [10] => 20 [11] => Red [12] => female [13] => [14] => 49 [15] => adm [16] => rec [17] => discharged [18] => history [19] => 1 [20] => 12 [21] => 5 [22] => abcd [23] => none [24] => no comment [25] => none1 [26] => Not Examined [27] => Normal [28] => Normal [29] => Normal [30] => Normal [31] => Normal [32] => Not Examined [33] => Normal [34] => Normal [35] => Normal [36] => Normal [37] => Normal [38] => Normal [39] => 37 [40] => good [41] => 34 [42] => no abnormalities [43] => etilogy [44] => diagnosis [45] => tentative [46] => final [47] => prognsis [48] => students [49] => clincian [50] => treatment [51] => Yes [52] => Yes [53] => Hb [54] => tc [55] => dc [56] => [57] => [58] => [59] => [60] => [61] => Yes [62] => Urineroutine [63] => [64] => [65] => [66] => [67] => [68] => Yes [69] => [70] => occultblood [71] => [72] => [73] => Yes [74] => [75] => GTT100gmGlucose [76] => Calcium [77] => [78] => [79] => [80] => [81] => [82] => [83] => [84] => [85] => [86] => [87] => [88] => [89] => [90] => [91] => [92] => [93] => [94] => [95] => [96] => Yes [97] => [98] => Potassium [99] => Chloride [100] => [101] => Yes [102] => [103] => LipidProfile [104] => RFT [105] => [106] => Yes [107] => [108] => [109] => [110] => [111] => [112] => GramsStain [113] => SkinClipSmear [114] => Toxoplasma [115] => [116] => [117] => [118] => [119] => No [120] => [121] => FNAC [122] => BodyFluidAnalysis [123] => [124] => Yes [125] => [126] => VitB12 [127] => T3T4TSH [128] => [129] => [130] => [131] => [132] => Yes [133] => [134] => XRay [135] => no aaasssssaw [136] => [137] => A [138] => s [139] => a [140] => s [141] => d [142] => f [143] => s )

$data1只有在完成fgetcsv($handle1)之后才会这样做 我的实际csv是

Id, Category,Patient Name,Email Id,Date,Telephone,Owner,Address,Species,Breed,Age,Colour,Gender,Immunization,Body Weight,ADM.By,REC. By,Discharged By,History Entered By,Number Affected,Num in Herd/Litter/ETC,Illness Duration,Diet,Previous Illness, Present Complaint ,Treatment (Previous) If any,General Appearance,Respiratory,Ears,Integument,Digestive,Nervous System,Musculo Skeletal,Genito- Urinary,Lymphatic System,Circulatory,Eyes,Uddar,Other,Temperature,Respiration,Pulse,Description of Abnormalities,Etiology,Differential Diagnosis,Tentative Diagnosis,Final Diagnosis,Prognsis,Students,Clinician,Treatment,Lab Examination Required,Haematology,Hb%,TC,DC,PlatelatesCount,ESR,Haemogram,A.E.C,Reticulocyte Count,Urine, Urine Routine,Urobilinogen,Urine Culture,Urine Micro Albuminurea,Ketone,Urine Protein Creatinine Ratio,Stool,Stool Routine,Occult Blood,Stool Preperation,Stool pH  ,Bio-Chemistry,FBS/PPBS/RBS,GTT100gmGlucose,Calcium,    Phosphorous,HDLCholesterol,Triglycerides,Cholestorol,Urea,Creatinine,Bun,Uric Acid, BilirubinTD,TotalProtein,Albumin,LDH,SGPT,SGOT,GGTP,AlkPo4,CPK,Amyless,Lipase,Electrolytes, Sodium,Potassium,Chloride,Bicarbonate,Profile,LFT,LipidProfile,RFT,DiabeticProfile,Microbiology:Serology,UrineCulture,StoolCulture,BloodCulture,PusCulture,SputumforAFB,GramsStain,SkinClipSmear,Toxoplasma,Typhidot,Troponin,Lepiospira,RAPIDElISA,Histopathology/Cytology Examination,    Cytology,FNAC,BodyFluidAnalysis,HPE,Hormones Test,  VitD3,VitB12,T3T4TSH,FsHLHProlactin,TestosteroneFT,Insulinlevel,FT3FT4TSH,Others,ECG,XRay,SpecialTest,Patient Immunizations,Date1,App,Bwls,Temp,Prog,Otp,Name of Clincian



54,xyz,Sara,saarah@gmail.com,16/03/18 ,,Sara,R T Nagar,BOVINE,Hallikar,20,Red,female,,49,adm,rec,discharged,history,1,12,5,abcd,none,no comment,none1,Not Examined,Normal,Normal,Normal,Normal,Normal,Not Examined,Normal,Normal,Normal,Normal,Normal,Normal,37,good,34,                                no abnormalities,etilogy,diagnosis,tentative,final,prognsis,students,clincian,treatment,Yes,Yes,Hb,tc,dc,,,,,,Yes,Urineroutine,,,,,,Yes,,occultblood,,,Yes,,GTT100gmGlucose,Calcium,,,,,,,,,,,,,,,,,,,,Yes,,Potassium,Chloride,,Yes,,LipidProfile,RFT,,Yes,,,,,,GramsStain,SkinClipSmear,Toxoplasma,,,,,No,,FNAC,BodyFluidAnalysis,,Yes,,VitB12,T3T4TSH,,,,,Yes,,XRay,no aaasssssaw,   ,  A       ,  s   , a   , s   , d   , f   , s


 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,   , b   , s   , s   , s   , s   , s   , s

0 个答案:

没有答案