如何在.txt文件中排除NULL结果并在php中显示它们?

我读取了一个包含这些值的csv文件:


; ;WH;391;M;9353970157191;A1124;0010;TU;1; ;7/1/2019;0;0;


; ;WH;391;M;9353970157214;A1119;0090;TU;1; ;7/1/2019;0;0;


....

最后,我得到了一个.txt文件,如下所示:


    MMXC1_|A0391|9353970395487|0|0


    MMXC1_|A0391|   |1|0 //$SKU_EAN NULL


    MMXC1_|A0391|9353970394879|4|2


...

但有时$SKU_EAN 为 NULL,因为它没有相应的sku_code


我想从我的.txt文件中排除$SKU_EAN为空的行,并将它们与sku_code以及csv文件中相应行的编号一起显示在屏幕上,我该怎么做?


$resultat = mysqli_query($bdd, "SELECT trim(concat(concat(SKU_ITEM_VARIANT,'_'),trim(SKU_SIZE)))as sku_code , SKU_EAN  FROM dwh_dev.dwh_d_sku"); 


while ($donnees[] = mysqli_fetch_assoc($resultat)) {


    // var_dump($donnees); //array(1) { [0]=> array(2) { ["sku_code"]=> string(13) "A1101_0090_TU" ["SKU_EAN"]=> string(13) "9346799868270" } } array(2) { [0]=> array(2) { ["sku_code"]=> string(13) "A1101_0090_TU" ["SKU_EAN"]=> string(13) "9346799868270" }....

}  


$constante = "MMXC1_";


$temp = array_column($datas_mag,  'MAGCOD', 'MAGAS400');

$temp2 = array_column($donnees,  'SKU_EAN', 'sku_code');



        if (($handle = fopen("$nomcsv", "r")) !== FALSE) { 


            $firstLine = true;


            while (($data = fgetcsv($handle, 1000000, ";")) !== FALSE) 

            {   

                if(!$firstLine) {


                    $EAN = 'A'.$temp[$data[3]];


                    $SKU_EAN = $temp2[$data[6].'_'.$data[7].'_'.$data[8]];

                    var_dump($SKU_EAN); //string(13) "9353970395487" string(13) "9346799046166" NULL NULL string(13) "9346799046756"...




$data_final[] = $constante.'|'.$EAN.'|'.$SKU_EAN.'|'.$data[12].'|'.$data[13];

 var_dump($data_final); //array(1) { [0]=> string(30) "MMXC1_|A0391|9353970395487|0|0" }

                 }

                 $firstLine = false;

            }   

        }


                 $cheminfile = "//alcyons/IT/PhotoShoot/retail/CSV/TXT_Finaux/MMX".date('His').".txt";


                $fp = fopen("$cheminfile", "w");


                foreach($data_final as $data){

                   fwrite($fp,$data."\n");

                }                           


                fclose($fp);


一只名叫tom的猫
浏览 181回答 1
1回答

呼啦一阵风

试试这个我假设您的注释是单个示例值,因此添加以下行应跳过此值为 null 的迭代。$SKU_EANif(is_null($SKU_EAN)) continue;完整代码:$i = 1;while(($data = fgetcsv($handle, 1000000, ";")) !== FALSE) {&nbsp; &nbsp;&nbsp; &nbsp; $i++;&nbsp; &nbsp; if(!$firstLine) {&nbsp; &nbsp; &nbsp; &nbsp; $EAN = 'A'.$temp[$data[3]];&nbsp; &nbsp; &nbsp; &nbsp; $SKU_EAN = $temp2[$data[6].'_'.$data[7].'_'.$data[8]];&nbsp; &nbsp; &nbsp; &nbsp; if(is_null($SKU_EAN)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Can\'t find SKU_EAN for iteration number: ' . $i . '<br />';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $data_final[] = $constante.'|'.$EAN.'|'.$SKU_EAN.'|'.$data[12].'|'.$data[13];&nbsp; &nbsp; }&nbsp; &nbsp; $firstLine = false;}&nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP