-2

I work on mws amazon api so i got data from ReportsApi with that request _GET_MERCHANT_LISTINGS_DATA_LITE_.

            $request = new MarketplaceWebService_Model_GetReportRequest($parameters);
    //Envoyer la demande GetReport et recuperer le requested report
    $getReportCont = self::invokeGetReport($service, $request);

    // Ouvrez le fichier et enregistrez les données de l'annonce
    $outDir = sprintf('%s\%s', $outBaseDir, date('Y'));
    if (!is_dir($outDir)) {
        if (!mkdir($outDir, 0777, true)) {
            die('Failed to create output Directory [makdir]...');
        }
    }
    //Creer nom de fichier
    $filename = sprintf('%s\Report_%s_%s.csv', $outDir, date('Ymd'), date('His'));
    $fp = fopen( $filename , 'w');
    //Ecrire dans le fichier les donnees de requested report
    fwrite($fp, $getReportCont);
    fclose($fp);

The problem is that data is not structured. So i save it on file.csv

sku-vendeur quantit�    prix    id-produit
10000   3   51,95   B00E3LV204
10002   3   85,96   B00PE9ZC1E
100024  3   345,73  B01LWMZ33O
100031  3   88,22   B018NOSAZ6

and from here i try to get a structured array in php.

I try many ways:

 $csv = explode("\n", file_get_contents('test.csv'));

 foreach ($csv as $key => $line)
 {
    $csv[$key] = str_getcsv($line);
 }

to obtain a array like this:

Array

{

[0] =>Array

{
    'sku-vendeur'=>"10000",
    'quantity'   =>"3",
    'prix' => "51,95",
    'id-produit'=>"B00E3LV204"
},
[1] =>Array
{
    'sku-vendeur'=>"10002",
    'quantity'   =>"3",
    'prix' => "85,96",
    'id-produit'=>"B00PE9ZC1E"
},
[2] =>Array
{
    'sku-vendeur'=>"100024",
    'quantity'   =>"3",
    'prix' => "345,73",
    'id-produit'=>"B01LWMZ33O"
}

But no way. Please someone can help me?Thank you very much an advance.

1 Answers1

0

This is how I would do it.
I would load the file as one string and use regex to parse the data.
Then I would loop one subarray and use the key in an array_column and array_combine to make it associative.

//$contents = file_get_contents($path); // commented since I don't have the actual file
$contents = "sku-vendeur quantit   prix    id-produit
10000   3   51,95   B00E3LV204
10002   3   85,96   B00PE9ZC1E
100024  3   345,73  B01LWMZ33O
100031  3   88,22   B018NOSAZ6";


preg_match_all("/^(.*?)\s+(.*?)\s+(.*?)\s+(.*?)$/m", $contents, $arr);

unset($arr[0]); // remove 0 since we don't need it.
$keys = array_column($arr, 0); // all headers are in 0

foreach($arr[1] as $key2 => $val){
    if($key2 != 0){ // don't do this with headers.
        $new[] = array_combine($keys,array_column($arr, $key2));
    }
}

var_dump($new);

output:

array(4) {
  [0]=>
  array(4) {
    ["sku-vendeur"]=>
    string(0) ""
    ["quantit"]=>
    string(5) "10000"
    ["prix"]=>
    string(1) "3"
    ["id-produit"]=>
    string(18) "51,95   B00E3LV204"
  }
  [1]=>
  array(4) {
    ["sku-vendeur"]=>
    string(0) ""
    ["quantit"]=>
    string(5) "10002"
    ["prix"]=>
    string(1) "3"
    ["id-produit"]=>
    string(18) "85,96   B00PE9ZC1E"
  }
  [2]=>
  array(4) {
    ["sku-vendeur"]=>
    string(0) ""
    ["quantit"]=>
    string(6) "100024"
    ["prix"]=>
    string(1) "3"
    ["id-produit"]=>
    string(18) "345,73  B01LWMZ33O"
  }
  [3]=>
  array(4) {
    ["sku-vendeur"]=>
    string(0) ""
    ["quantit"]=>
    string(6) "100031"
    ["prix"]=>
    string(1) "3"
    ["id-produit"]=>
    string(18) "88,22   B018NOSAZ6"
  }
}

https://3v4l.org/YgeRa

Andreas
  • 23,610
  • 6
  • 30
  • 62