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 NeRF的代码复现以及人工智能入门
  • ¥15 centos如何做一个可以让ikuai连接的openvpn
  • ¥15 Protege 中的Entities下面的界面删除了如何复原
  • ¥15 scrapy爬虫求帮
  • ¥15 imageEnView绘图问题
  • ¥15 关于#python#的问题:您好可以加您一下联系方式吗,在复现的时候确实有点问题难以解决
  • ¥15 联想电脑重装系统时无法发现硬盘
  • ¥15 MATLAB与UR10e实体机械臂建立通讯
  • ¥15 c++题需要快一点不用opencv
  • ¥15 关于#java#的问题:想要咨询Flowable流程引擎框架的问题