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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的