Home » Php » csv – How to insert in a .txt file a value from a php array?

csv – How to insert in a .txt file a value from a php array?

Posted by: admin February 25, 2020 Leave a comment

Questions:

In the variable $donnees I have

$donnees =

Array ( [0] => A1119_0090_TU [sku_code] => A1119_0090_TU [1] => 9346799046166 [SKU_EAN] => 9346799046166 )

When I read my csv file I form the A1119_0090_TU with “$data[6].’‘.$data[7].’‘.$data[8]”, I should find and insert in my .txt file the value 9346799046166 but it inserts a blank, where-is my error?

$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");

$donnees = mysqli_fetch_assoc($resultat); 

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


print_r ($donnees); 

   }

$constante = "MMXC1_";

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

        if (($handle = fopen("$nomcsv", "r")) !== FALSE) { 
            $firstLine = true;
            while (($data = fgetcsv($handle, 1000000, ";")) !== FALSE) 
            {   
                if(!$firstLine) {

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

                    // Create line here and immediately add it to `$data_final`
                    $data_final[] = $constante.'|'.$SKU_EAN.'|'.$data[12].'|'.$data[13]; 
                 }
                 $firstLine = false;
            }   
        }




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

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


            fputcsv($fp, $data_final,  "\n");                              

            fclose($fp);
How to&Answers: