Home » Php » csv – How to retrieve a value from my php array?

csv – How to retrieve a value from my php array?

Posted by: admin February 25, 2020 Leave a comment

Questions:

My query gives me a result in an array :

$donnnes =

Array ( [0] => Array ( [sku_code] => A1101_0090_TU [SKU_EAN] => 9346799868270 ) ) 

I put in comment what the var_dump of my variables give me, the variable SKU_EAN gives me a NULL value, how to recover the value of SKU_EAN (9346799868270) when it reads sku_code (A1101_0090_TU) in my csv file ?

 $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); 
    print_r($donnees); //Array ( [0] => Array ( [sku_code] => A1101_0090_TU [SKU_EAN] => 9346799868270 ) ) 


    $constante = "MMXC1_";

    $temp2 = array_column($donnees, 'sku_code', 'SKU_EAN');
    var_dump($temp2); //array(1) { ["9346799868270"]=> string(13) "A1101_0090_TU"

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

                $firstLine = true;

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

                        $sku_code = $data[6].'_'.$data[7].'_'.$data[8];
                        var_dump($sku_code); //A1101_0090_TU

                        $SKU_EAN = $temp2[$sku_code];
                        var_dump($SKU_EAN); //NULL

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

                    $delimiter = ';';

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

                    fclose($fp);    
How to&Answers:

You need to change the parameters in the function array_column

Use this

$temp2 = array_column($donnees,  'SKU_EAN', 'sku_code');
var_dump($temp2); //array(1) { ["A1101_0090_TU"]=> string(13) "9346799868270"}

Instead of

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