Tab-delimited PHP string to array

Mark Overton picture Mark Overton · Jul 4, 2018 · Viewed 12.4k times · Source

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) ""
}

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.

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.

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.

Answer

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

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

"\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.