制表符分隔的PHP字符串到数组 [英] Tab-delimited PHP string to array

查看:69
本文介绍了制表符分隔的PHP字符串到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GitHub上的AmazonMwsComplete存储库来向Amazon的AWS API(特别是GetReport调用)发出请求.我的请求得到了积极的回应,因此一切似乎都很好,唯一的问题是它似乎将XML回应转换为制表符分隔的字符串,我无法解决如何正确格式化为一个PHP数组.

I am using the AmazonMwsComplete repo on GitHub to make requests to Amazon's AWS API, specifically the GetReport call. I'm getting a positive response from my requests so it all seems to be working fine, the only issue is that it appears to transform the XML response into a tab-delimited string that I can't work-out how to correctly format into a PHP array.

本质上,响应看起来像这样:

Essentially, the response looks something like this:

string(52148) "sku     asin    price   quantity       Business Price Quantity Price Type        Quantity Lower Bound 1 Quantity Price 1       Quantity Lower Bound 2 Quantity Price 2 Quantity Lower Bound 3 Quantity Price 3       Quantity Lower Bound 4 Quantity Price 4 Quantity Lower Bound 5 Quantity Price 5       ProductTaxCode VatExclusivePrice        VatExclusiveBusinessPrice      VatExclusiveQuantityPrice1        VatExclusiveQuantityPrice2     VatExclusiveQuantityPrice3        VatExclusiveQuantityPrice4     VatExclusiveQuantityPrice5
XX-XXXX-XXXX   XXXXXXXXXX     0.01   1                                                                                                                                                        
XX-XXXX-XXX   XXXXXXXXXX     0.01    1                                                                                                                                                       
XXX-XXX-XXX    XXXXXXXXXX     0.01   1"

当我将其转换为数组时,它看起来像这样:

When I convert this into an array it looks like this:

array(22702) {
  [0]=>
  string(3) "sku"
  [1]=>
  string(4) "asin"
  [2]=>
  string(5) "price"
  [3]=>
  string(8) "quantity"
  [4]=>
  string(14) "Business Price"
  [5]=>
  string(19) "Quantity Price Type"
  [6]=>
  string(22) "Quantity Lower Bound 1"
  [7]=>
  string(16) "Quantity Price 1"
  [8]=>
  string(22) "Quantity Lower Bound 2"
  [9]=>
  string(16) "Quantity Price 2"
  [10]=>
  string(22) "Quantity Lower Bound 3"
  [11]=>
  string(16) "Quantity Price 3"
  [12]=>
  string(22) "Quantity Lower Bound 4"
  [13]=>
  string(16) "Quantity Price 4"
  [14]=>
  string(22) "Quantity Lower Bound 5"
  [15]=>
  string(16) "Quantity Price 5"
  [16]=>
  string(14) "ProductTaxCode"
  [17]=>
  string(17) "VatExclusivePrice"
  [18]=>
  string(25) "VatExclusiveBusinessPrice"
  [19]=>
  string(26) "VatExclusiveQuantityPrice1"
  [20]=>
  string(26) "VatExclusiveQuantityPrice2"
  [21]=>
  string(26) "VatExclusiveQuantityPrice3"
  [22]=>
  string(26) "VatExclusiveQuantityPrice4"
  [23]=>
  string(40) "VatExclusiveQuantityPrice5
XX-XXXX-XXXX"
  [24]=>
  string(10) "XXXXXXXXX"
  [25]=>
  string(5) "0.01"
  [26]=>
  string(1) "1"
  [27]=>
  string(0) ""
  [28]=>
  string(0) ""
  [29]=>
  string(0) ""
  [30]=>
  string(0) ""
}

这有一些问题,"VatExclusiveQuantityPrice5"标题与第一条信息(产品SKU)混合在一起,因此它位于数组的同一元素中.

There’s a few problems with this, the "VatExclusiveQuantityPrice5" heading is mixed in with the first piece of information which is the product SKU so it’s in the same element in the array.

制表符分隔的字符串还使用20个制表符作为换行符",因此在下一条信息之前,我最终在数组中包含20个空白元素.

The tab-delimited string also uses 20 tabs as a ‘new line’ break, so I end up with 20 blank elements in the array before the next piece of information.

对此,我有一个可能的解决方案,涉及将其转换为文本文件并直接导入数据库,但是肯定有一种简单的方法可以将其转换为PHP数组,数据库选项似乎要困难得多.去处理这些信息.

I have one possible solution to this that involves converting it into a text file and importing straight into a database, but surely there must be a simple way to convert this into a PHP array, the database option seems a far more difficult way to go about handling this information.

推荐答案

$myArray = [];
$lines = explode(PHP_EOL, $myText);
$l = 0;
foreach($lines as $line) {
    $myArray[$l] = explode("\t", $line);
    $l++;
}

PHP_EOL:换行符之一,"\ n","\ r"或"\ n \ r"

PHP_EOL: one of the new line delimiters, "\n", "\r" or "\n\r"

"\ t":'tab'字符,带有双引号,因此PHP可以将其解释为特殊字符,而不仅仅是字符串\ t

"\t": the 'tab' character, with double quotes so PHP can interpret it as a special character and not just the string \t

第一行是标题,下一行是表格内容.

The first line will be your headers, the next ones the table content.

这篇关于制表符分隔的PHP字符串到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆