dongmangsha7354 2018-07-27 13:22
浏览 87
已采纳

在php数组上转换mws结果.csv

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("
", 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条回答 默认 最新

  • dongtai419309 2018-07-27 14:15
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存